亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于Spring中JSON和對象的轉化

關于Spring中JSON和對象的轉化

30秒到達戰場 2019-07-08 04:04:57
有一個項目是這樣:從百度天氣API接口獲取json字符串,輸出到jsp頁面。要求用Spring框架開發。我的思路是設計一個dao從百度中獲取json,service層調用這個方法。然后在controller中將json轉化為weather對象注入jsp,jsp中使用${weather.xxx}的方式輸出天氣信息。請實現過程。主要是怎么將dao中的json轉化為weather返回//controller類似這樣:public String getWeather(String city, ModelMap model){Weather weather;//轉化過程//...............//..............String json = xxxxx.getJson(); model.addAttribute("weather", weather);return "weather.jsp";}我的意思不是要解析json,而是使用spring來實現。我知道有@RequesBody等等,但是不會用,好像并不能實現我的要求
查看完整描述

3 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

Form formPreview = new Form();
public Leaf(string name) : base(name) { }
public override void Add(Component c)
{
Console.WriteLine("Cannot add to a leaf");
}
public override void Remove(Component c)
{
Console.WriteLine("Cannot remove to a leaf");
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth)+name);
}
}

查看完整回答
反對 回復 2019-07-09
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

這個很簡單可以使用spring mvc自帶的jackson

1、web工程lib中加入jackson所需jar包:jackson-core-asl-1.9.9.jar、jackson-mapper-asl-1.9.9.jar

2、在applicationContext.xml中加入jackson的配置

1

2

3

4

<!-- json轉換器 -->

<bean id="jsonConverter"

 class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  <property name="supportedMediaTypes" value="application/json" />

</bean>

3、在你的action中直接使用注解的方式"@ResponseBody"自動將返回值轉化成json格式

1

2

3

4

5

6

7

8

9

@Controller

@RequestMapping("task")

public class TaskControl { 

     @RequestMapping("getUserById") 

     @ResponseBody

     public List getUserById(Integer id) {

         return this.taskService.getUserById(id);

     } 

}

4、jsp頁面的js寫法跟普通ajax格式一樣

1

2

3

4

5

functon getUserById(id){

    $.getJSON("task/getUserById",{id:id},function(data){

   

    });  

}



查看完整回答
反對 回復 2019-07-09
  • 3 回答
  • 0 關注
  • 1467 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號