在学校的呆了一段时间,马上又要回去工作了,不说了,我现在介绍一下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. linux虚拟机最优测试环境搭建

    目标:创建一个最优的linux虚拟机环境 环境:vmware12.0 系统:centos6.5 (* 以下配置是建立在配置完成基础网络环境后创建的,用static静态IP地址) 1.关闭selinux ...

  2. 插上翅膀,让Excel飞起来——xlwings(一)

    python操作Excel的模块,网上提到的模块大致有:xlwings.xlrd.xlwt.openpyxl.pyxll等,他们提供的功能归纳起来有两种:一.用python读写Excel文件,实际上就 ...

  3. ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

    A. Trip For Meal Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. ...

  4. Spark Streamming 基本输入流I(-) :File/Hdfs

    Spark Streamming 基本输入流I(-):从文件中进行读取 文件读取1:本地文件读取 这里我只给出实现代码及操作步骤 1.在本地目录下创建目录,这里我们创建目录为~/log/ 2.然后手动 ...

  5. Android学习笔记_71_Android 多个项目之间如何引用 项目怎样打jar包

    一.将整个项目作为资源文件 1.需要将被应用的项目设置为库项目. 2.将该项目的配置文件中的四大组件清空,例如下面代码: <?xml version="1.0" encodi ...

  6. HDU 1426 Sudoku Killer(dfs 解数独)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...

  7. jquery 跨域获取网页数据

    <script language="javascript" src="http://cbsahhs.blog.163.com/jquery.min.js" ...

  8. windows下搭建python

    windows下搭建python 下载python版本  https://www.python.org/   注意当前操作系统的位数,32位还是64位 同时   安装后  修改环境变量         ...

  9. springboot 整合dubbo 消费模块引入springboot 之后自动注入jdbc 模块导致启动报错问题

    方案一: 排除方法 pom文件直接将数据起步模块内排除数据源自动注入 jdbc jar <!--mybatis-plus 集成 --><!--mybitis--><dep ...

  10. JSTL&EL

    JSTL <1> 实现了JSP页面代码的复用 <2> 使得可读性更强 导入 <%@ taglib uri="http://java.sun.com/jsp/js ...