关于Spring中JSON和对象的转化

有一个项目是这样:从百度天气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等等,但是不会用,好像并不能实现我的要求

30秒到达战场
浏览 1389回答 3
3回答

三国纷争

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);}}

慕田峪7331174

这个很简单可以使用spring mvc自带的jackson1、web工程lib中加入jackson所需jar包:jackson-core-asl-1.9.9.jar、jackson-mapper-asl-1.9.9.jar2、在applicationContext.xml中加入jackson的配置1234<!--&nbsp;json转换器&nbsp;--><bean&nbsp;id="jsonConverter"&nbsp;class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">&nbsp;&nbsp;<property&nbsp;name="supportedMediaTypes"&nbsp;value="application/json"&nbsp;/></bean>3、在你的action中直接使用注解的方式"@ResponseBody"自动将返回值转化成json格式123456789@Controller@RequestMapping("task")public&nbsp;class&nbsp;TaskControl&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@RequestMapping("getUserById")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ResponseBody&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List&nbsp;getUserById(Integer&nbsp;id)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.taskService.getUserById(id);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;}4、jsp页面的js写法跟普通ajax格式一样12345functon&nbsp;getUserById(id){&nbsp;&nbsp;&nbsp;&nbsp;$.getJSON("task/getUserById",{id:id},function(data){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP