解析json串的两种方式

来源:互联网 发布:英雄联盟mac有国服? 编辑:程序博客网 时间:2024/06/05 05:25
import com.google.gson.Gson;
import org.json.JSONArray;import org.json.JSONObject;import org.junit.Assert;import org.junit.Test;

//response是一个json串
String response = resp.getEntity(String.class);
//第一种方式--使用Gson把json串转换成对象Gson gson =new Gson();BrowseArticleResult Result=gson.fromJson(response, BrowseArticleResult.class);Assert.assertEquals("50000",Result.getReturnCode());Assert.assertEquals("browseArticle",Result.getAction());
//第二种方式--把json串变成一个JSONObject对象进行操作JSONObject jsonObject = new JSONObject(response);JSONArray documentList = jsonObject.getJSONArray("documentList");JSONObject object= documentList.getJSONObject(0);Assert.assertEquals("不正常人类研究中心 Abnormal_human 好喜欢哦!!拥有皮克斯或迪斯尼风格的自己3D卡通形象", object.get("headline"));
0 0