Struts2 lib下六个包要用到的
json-lib-2.2.2-jdk15.jar
ezmorph-1.0.2.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang-2.0.jar
commons-logging-1.0.4.jar
我做测试用过的包
------------------------------------------------------------
当我们的前台的数据传递到后台的时候,如果前台的key值都存在于某个对象,那么,我们可以用
Gson.jar插件轻而易举的进行转换成对象,可是,往往我们的json字符串的key值不是一一对应的
下面就是json字符串的解析。
用java解析json字符串问题
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"-8℃","temp2":"4℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}
请问这个怎么解析呢?
------解决方案--------------------------------------------------------
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public void getJSONObject(){
JSONObject node = JSONObject.fromObject("{'weatherinfo':{'city':'北京','cityid':'101010100',
temp1':'-8℃','temp2':'4℃','weather':'晴','img1':'n0'}}");
List<Object> nodes = new ArrayList<Object>();
JSONArray jsons = JSONArray.fromObject(node.get("weatherinfo"));
for (Object o : jsons)
{
JSONObject jsonNode = JSONObject.fromObject(o);
List<Object> treeNodes = new ArrayList<Object>();
treeNodes.add(jsonNode.getString("city"));
treeNodes.add(jsonNode.getString("cityid"));
treeNodes.add(jsonNode.getString("temp1"));
treeNodes.add(jsonNode.getString("temp2"));
//...
nodes.add(treeNodes);
}
System.out.println(nodes);
}