在学校的呆了一段时间,马上又要回去工作了,不说了,我现在介绍一下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初体验(一):JsonObject解析的更多相关文章

  1. JSON初体验(三):FastJson解析

    JSON解析之FastJson(阿里巴巴解析开源) 特点: Fastjson是一个Java语言编写的高性能功能完善的JSON库,它采用的 是一种"假定有序快速匹配"的算法,把JSO ...

  2. JSON初体验(二):Gson解析

    今天,我们来介绍一下Gson的jar包的用法. JSON解析之Gson 特点:编码简介,谷歌官方推荐 数据之间的转换: 1.将json格式的字符串{}转换成为java对象 API: <T> ...

  3. SpringBoot初体验及原理解析

    一.前言 ​ 上篇文章,我们聊到了SpringBoot得以实现的幕后推手,这次我们来用SpringBoot开始HelloWorld之旅.SpringBoot是Spring框架对“约定大于配置(Conv ...

  4. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  5. Python+Flask+Gunicorn 项目实战(一) 从零开始,写一个Markdown解析器 —— 初体验

    (一)前言 在开始学习之前,你需要确保你对Python, JavaScript, HTML, Markdown语法有非常基础的了解.项目的源码你可以在 https://github.com/zhu-y ...

  6. $Java-json系列(二):用JSONObject解析和处理json数据

    本文中主要介绍JSONObject处理json数据时候的一些常用场景和方法. (一)jar包下载 所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre ( ...

  7. 用JSONObject解析和处理json数据

    (一)jar包下载 所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre (二)常见场景及处理方法 1.解析简单的json字符串: 1 // 简单的jso ...

  8. JSONObject解析json数据

    首先先看一下我们要解析的json数据是什么样子的: 代码: String url="http://113.57.190.228:8001/Web/Report/GetBigMSKReport ...

  9. 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...

随机推荐

  1. 一对多sql

    <!-- 分页查询派货成本 --> <select id="queryCostRegionPriceBycondtion" parameterMap=" ...

  2. May 27th 2017 Week 21st Saturday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I remember in the movie, The Da ...

  3. March 30 2017 Week 13 Thursday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. On the day, March 12th 2017, I ...

  4. 如何从ERP将Material的Batch信息下载到CRM并存储在settype COMM_PR_BATCH里

    前提条件:必须先确保三个对象ATTRIBUTE, CLASS和OBJCL成功下载.可以到事物码R3AM1里查看,确保状态全部为Done. (1) 在事物码MM02里,切换到视图classificati ...

  5. bootstrap table 主子表 局部数据刷新(刷新子表)

    1.主表中设置data-detail-view="true",启用主子表模式: <table class="table table-striped" wi ...

  6. Uva 11419 我是SAM

    题目链接:https://vjudge.net/problem/UVA-11419 题意:一个网格里面有一些目标,可以从某一行,某一列发射一发子弹,可以打穿: 求最少的子弹,和在哪里打? 分析: 听说 ...

  7. 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory li ...

  8. 【洛谷P1159】排行榜

    排行榜 题目链接 看到题解中一个很巧妙的做法: 先确定SAME的位置, 将DOWN的按输入顺序从上往下输出 再将UP的接着从上往下输出 这样便可以保证DOWN的人名次一定下降了 UP的人名次一定上升了 ...

  9. 【luogu P1343 地震逃生】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1343 菜 #include <queue> #include <cstdio> #i ...

  10. 【luogu P3178 [HAOI2015]树上操作】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3178 模板题 菜 #include <cstdio> #include <cstring& ...