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. 网络协议之HTTP协议

    HTTP协议详解(真的很经典) 转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HT ...

  2. Python标准库笔记(2) — re模块

    re模块提供了一系列功能强大的正则表达式(regular expression)工具,它们允许你快速检查给定字符串是否与给定的模式匹配(match函数), 或者包含这个模式(search函数).正则表 ...

  3. rhel-server srpms iso

    http://ftp.redhat.com/pub/redhat/linux/enterprise/7Server/en/ ftp://ftp.pslib.cz/pub/linux/redhat-cz ...

  4. tornado 响应头 中断 状态码 工作流程

    set_header    设置响应头 clear_header 清除响应头 add_header   增加响应头 self.flush  self.finish  中断 set_status     ...

  5. java.util.ConcurrentModification并发修改异常

    在运行下面这段代码时出现了并发修改异常java.util.ConcurrentModification: public static void main(String[] args) { List l ...

  6. 算法入门系列1:k-means

    k-means是一种无监督学习算法,用于聚类. 下图(来自http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006910.html)展示了k-m ...

  7. Centos7 环境准备

    Centos7 环境准备 #关闭防火墙 systemctl stop firewalld systemctl disable firewalld #关闭selinux sed -i 's/SELINU ...

  8. linux命令(4):vmstat命令

    CPU监控如下: vmstat 2 10  //表示每隔2秒运行10次 内存监控如下: vmstat –s 监控进程及CPU.内存状态 如下: top

  9. POJ 1984 Navigation Nightmare(二维带权并查集)

    题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1  F2  L  D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...

  10. MySQL的七种join

    转载 原文地址 建表 在这里我们先建立两张有外键关联的两张表: CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept` ...