[转]json+JSONObject+JSONArray 结合使用
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("===================================+++++++++++++++++++++");
}
}
最新文章
- BZOJ4567[Scoi2016]背单词
- Codeforces558E A Simple Task(线段树)
- zookeeper+jstorm的集群搭建
- python引用py文件中文报错
- Logstash安装及部署
- 关联规则挖掘之apriori算法
- UIButton setImage setBackgoundImage
- .net web 点击链接在页面指定位置显示DIV的问题
- Windows Azure 的虚拟硬盘和文件的相关概念
- MFC六大关键技术
- C# 串口接收数据中serialPort.close()死锁
- C#多线程Thread.Join()的详解
- APP-FND-00676: 弹性域例程 FDFGDC 无法读取为此说明性弹性域指定的默认引用字段
- LDA实现
- python自动化开发-[第六天]-常用模块、面向对象
- MySql-8.0.12 安装教程
- Java String对象的问题 String s=";a";+";b";+";c";+";d";
- TextView展示富文本时emoj或图片和文字不对齐的解决方案
- Docker入门学习总结
- java study3
热门文章
- java的应用项目
- Deep Learning基础--CNN的反向求导及练习
- Shell——Linux/Mac 终端复制文件内容到剪切板
- (转)函数后面加const--C++ const成员函数
- Android UI 设计:pixel dip dpi sp density
- php的设计模式------工厂模式
- windows下github 出现Permission denied (publickey)
- python基础(4)---解释器、编码、字符拼接
- 《深入浅出MyBatis技术原理与实战》——4. 映射器,5. 动态SQL
- hdu 1430(BFS+康托展开+映射+输出路径)