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. Coursera课程《Machine Learning》吴恩达课堂笔记

    强烈安利吴恩达老师的<Machine Learning>课程,讲得非常好懂,基本上算是无基础就可以学习的课程. 课程地址 强烈建议在线学习,而不是把视频下载下来看.视频中间可能会有一些问题 ...

  2. Linux 入门记录:十六、Linux 多命令协作:管道及重定向

    一.多命令协作 在 Linux 系统当中,大多数命令都很简单,很少出现复杂功能的命令,每个命令往往只实现一个或多个很简单的功能.通过将不同功能的命令组合一起使用,可以实现某个复杂功能的. Linux ...

  3. 在64位linux下编译32位程序

    在64位linux下编译32位程序 http://blog.csdn.net/xsckernel/article/details/38045783

  4. [LabVIEW架构]ActorFramework(一)

    前言 小黑结婚回来第二周了,每天忙于程序设计,时间比较紧张,所以文章一直没出来,也算憋大招了. 近期小黑将与大家一起认识一下ActorFramework,既是对自己一段时间写AF程序的总结,也是梳理, ...

  5. js对页面对float类型的数据格式化处理

    <script>        document.write(parseFloat(<s:property value="assureterm"/>)); ...

  6. Volatile arrays in Java

    Volatile arrays in Java A slight complication of Java volatile fields, and one sometimes overlooked, ...

  7. python 函数的几个属性 func_name, func_code等

    直接见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/07/25 10:14 def add(x=0, y=1): & ...

  8. Go语言标准包之json编码

    标准的就简单通用. package main import ( "encoding/json" "fmt" "log" ) func mai ...

  9. AC日记——背单词 洛谷 P2353

    背单词 思路: KMP+统计前缀和优化: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 ], ...

  10. AC日记——聪明的质监员 洛谷 P1314

    聪明的质监员 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define l ...