博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]json+JSONObject+JSONArray 结合使用
阅读量:4458 次
发布时间:2019-06-08

本文共 2918 字,大约阅读时间需要 9 分钟。

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("===================================+++++++++++++++++++++");
  
  
 }

}

 

转载于:https://www.cnblogs.com/ZhuRenWang/p/4765524.html

你可能感兴趣的文章
jquery下php与ajax的数据交换方式
查看>>
魅蓝Note有几种颜色 魅蓝Note哪个颜色好看
查看>>
使用PullToRefresh实现下拉刷新和上拉加载
查看>>
透明度百分比与十六进制转换
查看>>
HBase表预分区
查看>>
NSBundle,UIImage,UIButton的使用
查看>>
vue-cli3 中console.log报错
查看>>
GridView 中Item项居中显示
查看>>
UML类图五种关系与代码的对应关系
查看>>
如何理解作用域
查看>>
从无到满意offer,你需要知道的那些事
查看>>
P1516 青蛙的约会 洛谷
查看>>
SDOI2011 染色
查看>>
JQuery EasyUI combobox动态添加option
查看>>
面向连接的TCP概述
查看>>
前端快捷方式 [记录]
查看>>
亲测可用,解决端口被占用的指令!!
查看>>
MySQL--视图、触发器、事务、存储过程、内置函数、流程控制、索引
查看>>
Django--数据库查询操作
查看>>
自定义配置文件的使用
查看>>