JSONObject与JSONArray的区别简述:

区别在于JSONObject是一个{}包裹起来的一个对象(Object),
而JSONArray则是[]包裹起来的一个数组(Array),
说白点就是一个是数组一个是对象或字符串。 例1:

package com.rtt.lltest;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;

import com.rtt.platform.system.util.JSONUtil;

import net.sf.json.JSONObject;

public class Test {
 
  public static void main(String[] args) {
  // Object 类型字符串
  String json =
"{\"name\":\"reiz\",\"age\":\"32\"}";
  JSONObject jsonObj =
JSONObject.fromObject(json);
  String name = jsonObj.getString("name");
  System.out.println(name+"||||||||||||||");
  // 结果:reiz
  jsonObj.put("initial", name.substring(0,
1).toUpperCase());
  // jsonObject 添加数组
  String[] likes = new String[] { "JavaScript",
"Skiing", "Apple Pie" };
  jsonObj.put("likes", likes);
  // jsonObject 添加Map
  Map<String,
String> ingredients = new
HashMap<String, String>();
  ingredients.put("apples", "3kg");
  ingredients.put("sugar", "1kg");
  ingredients.put("pastry", "2.4kg");
  ingredients.put("bestEaten", "outdoors");
  jsonObj.put("ingredients",ingredients);
 
  System.out.println(jsonObj);
 
System.out.println(jsonObj.getString("likes"));
 
System.out.println(jsonObj.getString("ingredients"));
 
  org.json.JSONObject jsonObje = new
org.json.JSONObject();
  JSONUtil.put(jsonObje, "perNum", "lisi");
  JSONUtil.put(jsonObje, "cardNum",
"12345600");
  org.json.JSONObject jsonObjee = new
org.json.JSONObject();
  JSONUtil.put(jsonObjee, "perNum",
"lilei");
  JSONUtil.put(jsonObjee, "cardNum",
"123456");
  JSONArray jsonArray = new JSONArray();
  jsonArray.put(jsonObje);
  jsonArray.put(jsonObjee);
 
System.out.println("---------------------------------");
  System.out.println(jsonArray.toString());
 
  }
}

=========================================================

JSONUtil.java

public static void put(JSONObject jsonObj, String key, Object
value) {

if (value == null) {

jsonObj.put(key, StringPool.BLANK);

}

else {

jsonObj.put(key, value.toString());

}

}

例2:JSONArray和JSONObject互相添加

package com.rtt.lltest;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class ObjTest {

public static void main(String[] args) {
  // TODO Auto-generated method
stub
  
  JSONObject jo = new
JSONObject();
  jo.put("isleaf", true);
  jo.put("name",
"zhangsan");
  jo.put("age", "25");
  
  JSONObject jo2 = new
JSONObject();
  jo2.put("isleaf", false);
  jo2.put("name", "lisi");
  jo2.put("age", "25");
  
  JSONObject jo3 = new
JSONObject();
  jo3.put("isleaf", true);
  jo3.put("name", "lisi");
  jo3.put("age", "25");
  
  JSONArray ja0 = new
JSONArray();
  //把JSONObject添加到中JSONArray

ja0.add(jo3);
  //把JSONArray添加到JSONObject中

jo2.element("children",
ja0);
  System.out.println(jo2.toString());

JSONArray ja1 = new
JSONArray();
  ja1.add(jo);
  ja1.add(jo2);
  ja1.add(jo3);
  System.out.println("===================================+++++++++++++++++++++");

System.out.println(ja1.toString());

System.out.println("===================================+++++++++++++++++++++");

}

}

[转]json+JSONObject+JSONArray 结合使用的更多相关文章

  1. jsonObject jsonArray jsonTokener jsonStringer,json解析以及http请求获取josn数据并加以解析

    JSON的定义: 一 种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的 支持),从而可以在不同平台间进行 ...

  2. java 字符串解析为json 使用org.json包的JSONObject+JSONArray

    参考: https://blog.csdn.net/xingfei_work/article/details/76572550 java中四种json解析方式 JSONObject+JSONArray ...

  3. net.sf.json JSONObject与JSONArray使用实例

    实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如i ...

  4. net.sf.json JSONObject与JSONArray总结

    JSONObject:json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} JSONArray:json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式 ...

  5. JSONObject,JSONArray,Map,String之间转换

    http://blog.csdn.net/superit401/article/details/51727739 1.String转JSONObject String jsonMessage = &q ...

  6. java JSONObject/JSONArray详解

    应用架包:json-lib-2.4-jdk15.jar.及相关依赖架包. 一.JSONObject和JSONArray对象 -------------------------------------- ...

  7. jsonobject 遍历 org.json.JSONObject

    import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public static  ...

  8. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  9. jsonObject jsonarray

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

随机推荐

  1. 2017-2018-1 20179205《Linux内核原理与设计》第五周作业

    <Linux内核原理与设计>第五周作业 视频学习及操作分析 一.用户态.内核态和中断 内核态在CPU执行中对应高执行级别,执行级别为0级,具有特权指令,可以访问任意物理地址:用户态执行级别 ...

  2. Oracle 集合

    --合并(UNION.UNION ALL) select * from empwhere ename like '%A%'unionselect * from empwhere ename like ...

  3. 【LA3882】And then there was one

    做sb题也是一种乐趣,是吧…… #include<bits/stdc++.h> #define N 10005 using namespace std; int f[N],m,n,k; i ...

  4. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  5. Jquery屏蔽浏览器的F1-F12快捷键,在IE,GOOGLE下测试均无问题

    在网上找了找,很多都是js实现的,东找西找,再加上自己的想法也勉强的完成了,直接看代码 <script type="text/javascript" src="Sc ...

  6. 【Java基础】重写equals需要重写hashcode

    Object里的equals用来比较两个对象的相等性,一般情况下,当重写这个方法时,通常有必要也重写hashcode,以维护hashcode方法的常规协定,或者说这是JDK的规范,该协定声明相等对象必 ...

  7. 在一个Ubuntu系统上配置Apache支持多个站点

    查看原文请访问:http://codewenda.com/ubuntu16-04%E9%85%8D%E7%BD%AEapache%E6%94%AF%E6%8C%81%E5%A4%9A%E4%B8%AA ...

  8. Leetcode 之Length of Last Word(38)

    做法很巧妙.分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶.注意最后要判断堆栈是否为空. bool isValid(const string& s) { ...

  9. 3.4Code

    #include<algorithm> #include<iostream> #include<cstdio> #define inf 0x3f3f3f3f #de ...

  10. linux命令(16):mv命令

    移动文件:mv /mnt/test.log /home 移动目录:mv -f /mnt/test /home [带-f参数如目的已存在同名文件,则直接覆盖掉] 文件改名:mv /mnt/test /m ...