JSON初体验
Posted 蜗牛不爱海绵宝宝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON初体验相关的知识,希望对你有一定的参考价值。
在学校的呆了一段时间,马上又要回去工作了,不说了,我现在介绍一下json相关的内容
1.JSON数据格式(总的来说,json就是一个字符串)
1.整体结构
String json1 = "{"id":12,"name":"Tom"}";
String json2 = "[{"id":12,"name":"Tom"},{"id":12,"name":"Tom"}]";
2.json对象
a.json对象的结构:{key1:value1,key2:value2,key3:value3}
b.key的数据类型:字符串
c.value的数据类型:
1.数值
2.字符串
3.null
4.json数组[]
5.json对象{}
d.例子
正确的:{“name”:"TOM","age":12}
错误的:{"aa":"a",3}
3.json数组
a.json数组的结构:[value1,value2,value3]
b.value的数据类型:
1.数值
2.字符串
3.null
4.json数组[]
5.json对象{}
c.例子
正确的:[1,"ab",[],{"n":132,“b”:"abc"}]
错误的:[1,"a":3]
2.JSON解析方向
1.将java对象(包含集合)转换为json格式字符串(在服务器端应用)
2.将json格式字符串转换为java对象,包含集合(在客户端应用)
3.json和java之间的转换关系
a.json对应java对象
b.json数组与java对象构成的list相对应
3.JSON解析技术
1.android原生技术:
特点:编程相对麻烦
数据之间转换(demo):
a.将json格式的字符串{}转换为java对象
API:
JSONObject(String json):将json字符串解析为json对象
Xxx getXxx(String name):根据name,在json对象中得到对应的Value
Xxx optXxx(String name):根据name,在json对象中得到对应的value
注意:
optXxx方法会在对应的key中的值不存在的时候返回一个空字符串或者返回
你指定的默认值,但是getString方法会出现空指针异常的错误
String json = "{\n" + " \"id\":2,\n" + " \"name\":\"Tom\",\n" + " \"price\":12.3,\n" + " \"imagePath\":\"http://www.baidu.com\"\n" + "}" JSONObject jsonObject = new JSONObject(json); int id = jsonObject.optInt("id"); String name = jsonObject.optString("name"); double price = jsonObject.optDouble("price"); String imagePath = jsonObject.optString("imagePath");
b.将json格式的字符串[]转换为java对象的List
API:
JSONArray(String json):将json字符串解析为json数组
int length():得到json数组中元素的个数
Xxx getXxx(int index):根据下标得到json数组中对应的元素数据
Xxx optXxx(int index):根据下标得到json数组中对应的元素数据
注意:
optXxx方法会在对应的key中的值不存在的时候返回一个空字符串或者返回
你指定的默认值,但是getString方法会出现空指针异常的错误
String json = "[\n" + " {\n" + " \"id\":1,\n" + " \"imagePath\":\"www.baidu.com\",\n" + " \"name\":\"Tom1\",\n" + " \"price\":12.3\n" + " },\n" + " {\n" + " \"id\":1,\n" + " \"imagePath\":\"www.douban.com\",\n" + " \"name\":\"Tom2\",\n" + " \"price\":12.5\n" + " }\n" + "]"; List<First> list = new ArrayList<First>(); JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); if(jsonObject != null){ int id = jsonObject.optInt("id"); String imagePath = jsonObject.optString("imagePath"); String name = jsonObject.optString("name"); Double price = jsonObject.optDouble("price"); First first = new First(id, name, price, imagePath); list.add(first); } } for (int i = 0; i <list.size() ; i++) { System.out.println(list.get(i).toString()); }
c.复杂json数据解析
String json = "{\n" + " \"data\":{\n" + " \"count\":5,\n" + " \"items\":[\n" + " {\n" + " \"id\":45,\n" + " \"title\":\"坚果\"\n" + " },\n" + " {\n" + " \"id\":132,\n" + " \"title\":\"炒货\"\n" + " },\n" + " {\n" + " \"id\":166,\n" + " \"title\":\"蜜饯\"\n" + " },\n" + " {\n" + " \"id\":195,\n" + " \"title\":\"果脯\"\n" + " },\n" + " {\n" + " \"id\":196,\n" + " \"title\":\"礼盒\"\n" + " }\n" + " ]\n" + " },\n" + " \"rs_code\":\"1000\",\n" + " \"rs_msq\":\"success\"\n" + "}"; List<Second> list = new ArrayList<Second>(); JSONObject jsonObject = new JSONObject(json); JSONObject data = jsonObject.optJSONObject("data"); String rs_code = jsonObject.optString("rs_code"); String rs_msq = jsonObject.optString("rs_msq"); int count = data.optInt("count"); JSONArray items = data.optJSONArray("items"); for (int i = 0; i < items.length(); i++) { JSONObject jj = items.getJSONObject(i); int id = jj.optInt("id"); String title = jj.optString("title"); Second second = new Second(id,title); list.add(second);
}
d.特殊json数据解析
FilmInfo类
public class FilmInfo { private int code; private List<FilmBean> list; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public List<FilmBean> getList() { return list; } public void setList(List<FilmBean> list) { this.list = list; } public static class FilmBean{ private String aid; private String author; private int coins; private String copyright; private String create; public String getAid() { return aid; } public void setAid(String aid) { this.aid = aid; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getCoins() { return coins; } public void setCoins(int coins) { this.coins = coins; } public String getCopyright() { return copyright; } public void setCopyright(String copyright) { this.copyright = copyright; } public String getCreate() { return create; } public void setCreate(String create) { this.create = create; } @Override public String toString() { return "FilmBean{" + "aid=‘" + aid + ‘\‘‘ + ", author=‘" + author + ‘\‘‘ + ", coins=" + coins + ", copyright=‘" + copyright + ‘\‘‘ + ", create=‘" + create + ‘\‘‘ + ‘}‘; }
} @Override public String toString() { return "FilmInfo{" + "code=" + code + ", list=" + list + ‘}‘; } }
此时对json进行分析
String json = "{\n" + " \"code\":0,\n" + " \"list\":{\n" + " \"0\":{\n" + " \"aid\":\"6008965\",\n" + " \"author\":\"哔哩哔哩番剧\",\n" + " \"coins\":170,\n" + " \"copyright\":\"Copy\",\n" + " \"create\":\"2016-08-25 21:34\"\n" + " },\n" + " \"1\":{\n" + " \"aid\":\"6008938\",\n" + " \"author\":\"哔哩哔哩番剧\",\n" + " \"coins\":404,\n" +r " \"copyright\":\"Copy\",\n" + " \"create\":\"2016-08-25 21:33\"\n" + " }\n" + " }\n" + "}"; FilmInfo filmInfo = new FilmInfo(); JSONObject jsonObject = new JSONObject(json); int code = jsonObject.optInt("code"); filmInfo.setCode(code); List<FilmInfo.FilmBean> filmList = new ArrayList<FilmInfo.FilmBean>(); filmInfo.setList(filmList); JSONObject list = jsonObject.optJSONObject("list"); for (int i = 0; i <list.length() ; i++) { JSONObject jsonObject1 = list.optJSONObject(i + ""); String aid = jsonObject1.optString("aid"); String author = jsonObject1.optString("author"); int coins = jsonObject1.optInt("coins"); String copyright = jsonObject1.optString("copyright"); String create = jsonObject1.optString("create"); FilmInfo.FilmBean filmBean = new FilmInfo.FilmBean(); filmBean.setAid(aid); filmBean.setAuthor(author); filmBean.setCoins(coins); filmBean.setCopyright(copyright); filmBean.setCreate(create); filmList.add(filmBean); } System.out.println(filmInfo);
最后在补充一点,这里面的org.json.jsonObject的maven为
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
以上是关于JSON初体验的主要内容,如果未能解决你的问题,请参考以下文章