上一篇文章当中,写了关于Java中使用fastjson创建json数据方法,这篇文章写写java中使用fastjson解析json数据的方法。
首先,老样子,还是需要先导入fastjson的jar包,这个jar包可以从百度上搜索然后下载,下载后导入就可以正常使用了。(导包的时候注意要使用alibaba为名的)
使用fastjson解析json数据的方法
第一种方法
第一步:
先将String 类型转换成jsonObject类型。
处理代码如下:
//首先定义一个String类型的数据,用力模拟发送过来的json数据。
String jsonStr = "{\"msg\":\"ok\",\"code\":200,\"data\":[{\"id\":1,\"userName\":\"fish1\",\"userPsw\":\"123456\"},{\"id\":2,\"userName\":\"fish2\",\"userPsw\":\"123456\"}]}";
//string 转换成jsonObject类型
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
第二步:
如果只要输出json中的单个键值的数据,直接使用get方法即可。
例如:
int code = jsonObject.getInteger("code");
String msg = jsonObject.getString("msg");
第三步:
要是需要将data中的数组集合解析出来,需要将其赋新值为json数组。然后再从json数组中通过get类型()方法提取出具体的键值。
具体代码如下:
JSONArray data = jsonObject.getJSONArray("data");
for(int i=0;i<data.size();i++){
JSONObject object = (JSONObject) data.get(i);
Integer id = object.getInteger("id");
String userName = object.getString("userName");
String userPsw = object.getString("userPsw");
JsonUser jsonUser = new JsonUser(id,userName,userPsw);
System.out.println(jsonUser);
}
完整代码呈现:
public static void main(String[] args){ String jsonStr = "{\"msg\":\"ok\",\"code\":200,\"data\":[{\"id\":1,\"userName\":\"fish1\",\"userPsw\":\"123456\"},{\"id\":2,\"userName\":\"fish2\",\"userPsw\":\"123456\"}]}"; //string 转换成jsonObject类型 JSONObject jsonObject = JSONObject.parseObject(jsonStr); // int code = (int) jsonObject.get("code"); int code = jsonObject.getInteger("code"); String msg = jsonObject.getString("msg"); //下面将data的集合解析出来 JSONArray data = jsonObject.getJSONArray("data");
for(int i=0;i&lt;data.size();i++){ JSONObject object = (JSONObject) data.get(i); Integer id = object.getInteger("id"); String userName = object.getString("userName"); String userPsw = object.getString("userPsw"); JsonUser jsonUser = new JsonUser(id,userName,userPsw); System.out.println(jsonUser); }</code></pre>
第二种方法
第二种方法相对第一种方法会更加简洁,但是需要新建一个类去储存数据。(注意,这里类中的data的数据是基于另一个User类) 第一步: 将String类型的json数据先做一个解析,但是这次解析和上次不一样,这次是将json数据解析成一个类的json结果集。 注意:这个类的变脸名字需要和json数据上面的键值一样,否则无法进行对象的新建 代码呈现:
//同样,先新建一个json数据,用于后面接受前面的数据。 String jsonStr = "{\"msg\":\"ok\",\"code\":200,\"data\":[{\"id\":1,\"userName\":\"fish1\",\"userPsw\":\"123456\"},{\"id\":2,\"userName\":\"fish2\",\"userPsw\":\"123456\"}]}"; JsonResult result = JSONObject.parseObject(jsonStr,JsonResult.class); //第一个是String 类型的json数据,后面哪个是用于创建对象的类
第二步: 然后就可以通过上面新建的result对象进行访问数据了。
//输出"ok" System.out.println(result.getMsg());
输出data集合中的数据
//将获取到的data集合赋值新建对象 List<JsonUser> userData = result.getData(); System.out.println(userData.get(1).getUserName());
完整代码展示:
public static void main(String[] args){ String jsonStr = "{\"msg\":\"ok\",\"code\":200,\"data\":[{\"id\":1,\"userName\":\"fish1\",\"userPsw\":\"123456\"},{\"id\":2,\"userName\":\"fish2\",\"userPsw\":\"123456\"}]}"; //string 转换成jsonObject类型 JsonResult result = JSONObject.parseObject(jsonStr,JsonResult.class); System.out.println(result.getMsg());
//将获取到的data集合赋值新建对象 List&amp;lt;JsonUser&amp;gt; userData = result.getData(); System.out.println(userData.get(1).getUserName()); //输出fish2 }&lt;/code&gt;&lt;/pre&gt; &lt;br&gt;