JSONObjectSample
package com.egeniuss.platform.basic; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONObjectSample {
/**
* 创建JSONObject对象 入参map JSON节点为map.key jSON节点值为map.get(key)
**/ public static JSONObject createJSONObject(Map reJsonMap) {
JSONObject jsonObject = new JSONObject();
if (reJsonMap != null && reJsonMap.size() > 0) {
Set keySet = reJsonMap.keySet();
Iterator ite = keySet.iterator();
String name = "";
while (ite.hasNext()) {
name = (String) ite.next();
System.out.println("key:" + name);
jsonObject.put(name, reJsonMap.get(name));
}
}
return jsonObject;
} // 创建JSONObject对象
public JSONObject createJSONObject(Object reJson) {
JSONObject jsonObject = JSONObject.fromObject(reJson);
return jsonObject;
} /**
* 解析JSONObject对象 入参:string(jSON格式),属性名称 出参 String
*/
public String decodeJSONObject(String resJson, String name) {
String nameValue = "";
JSONObject reqObj = JSONObject.fromObject(resJson);
nameValue = reqObj.getString(name);
nameValue = nameValue == null ? "" : nameValue;
return nameValue;
} /**
* 解析JSONObject对象 入参:JSONObject,属性名称 出参 String
*/
public String decodeJSONObject(JSONObject resJson, String name) {
String nameValue = "";
nameValue = resJson.getString(name);
nameValue = nameValue == null ? "" : nameValue;
return nameValue;
} /**
* 解析JSONObjectArray对象 入参:JSONObject,属性名称 出参 String
*/
public List decodeJSONArray(String resJson, String name) {
List list = new ArrayList();
JSONObjectSample js = new JSONObjectSample();
JSONArray jsonArr = JSONArray.fromObject(js.decodeJSONObject(resJson,
name));
for (int i = 0; i < jsonArr.size(); i++) {
String[] array = new String[2];
array[0] = js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
"createTime");
array[1] = js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
"description");
System.out.println("bpm=" + array[0] + "dpm" + array[1]);
}
return list;
} public static void main(String[] args) {
// 输出jsonobject对象
Map map = new HashMap();
map.put("aaaaaa", "awdqweq");
map.put("bbbb", "awdqweq");
map.put("ccc", "awdqweq");
map.put("fdd", "awdqweq");
map.put("aaxxxaaaa", "awdqweq");
map.put("aaaaewqeaa", "awdqweq");
map.put("aaaadawdaa", "awdqweq");
map.put("eweq", "awdqweq");
JSONObject jsonObject = JSONObjectSample.createJSONObject(map);
System.out.println("map的长度" + map.size());
System.out.println("jsonObject==>" + jsonObject);
// 判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:" + isArray + " isEmpty:" + isEmpty
+ " isNullObject:" + isNullObject);
//
// //添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>" + jsonObject); // 返回一个JSONArray对象
String a = "{\"resultCode\":\"0\",\"msg\":\"\",\"data\":[{\"createTime\":\"2014-02-22 19:13:27\",\"description\":\"理赔受理\"}]}";
JSONObjectSample js = new JSONObjectSample();
// a = js.decodeJSONObject(a, "data");
// System.out.println(a);
// jsonObject.decodeJSONObject(a,"resultCode");
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1, "another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
// jsonArray=jsonObject.getJSONObject(a);
// JSONArray jsonArr = JSONArray.fromObject(a);
// String s= array.getString(1);
// for (int i = 0; i < jsonArr.size(); i++)
// {
// String dpm= js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
// "description");
// System.out.println("bpm="+bpm+"dpm"+dpm);
// }
js.decodeJSONArray(a, "data");
// String json =
// "[{\"a\":\"111\",\"b\":\"222\",\"c\":\"333\"},{\"a\":\"1000\",\"b\":\"2000\",\"c\":\"000\"},{\"a\":\"999\",\"b\":\"300\",\"c\":\"700\"}]";
// JSONArray jsonArr = JSONArray.fromObject(json);
// String a1[] = new String[jsonArr.size()];
// String b[] = new String[jsonArr.size()];
// String c[] = new String[jsonArr.size()];
// for (int i = 0; i < jsonArr.size(); i++) {
// a1[i] = jsonArr.getJSONObject(i).getString("a");
// b[i] = jsonArr.getJSONObject(i).getString("b");
// c[i] = jsonArr.getJSONObject(i).getString("c");
// }
//
// for (int i = 0; i < c.length; i++) {
// System.out.print(a1[i]+" ");
// System.out.print(b[i]+" ");
// System.out.print(c[i]);
// System.out.println();
// }
// //添加JSONArray后的值
// {"resultCode":"0","msg":"","data":[{"createTime":"2014-02-22 19:13:27","description":"理赔受理"}]}
// {"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
// "jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject);
//
// //根据key返回一个字符串
// String jsonString = jsonObject.getString("name");
// System.out.println("jsonString==>"+jsonString);
//
// 根据key返回一个字符串
// String decodeString = JSONObjectSample.decodeJSONObject(jsonObject,
// "eweq");
// System.out.println("decodeString==>"+decodeString);
// RespContext respContext = new RespContext();
// respContext.setAlipayUserId("dawweq");
// respContext.setComId("weqwe2312");
// JSONObject jsonObjec1t =
// JSONObjectSample.createJSONObject(respContext);
System.out.println("jsonObjec1t==>" + jsonObject); } }
JSONObjectSample的更多相关文章
- JSONObject
JAR包简介: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.j ...
- json---简单入门
1.推荐使用第三种方式JAVABEAN的方式(使用前引入org.json) package day05; import java.util.HashMap; import java.util.Map; ...
- 日志——JSON的相关方法
http://www.cnblogs.com/henryxu/archive/2013/03/10/2952738.html JSON jar包: commons-lang.jar commons- ...
- jsonObject jsonarray
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- JSONObject与JSONArray的使用
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- [转]JSONObject与JSONArray的使用
http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html 参考文献: http://blog.csdn.net/huangwuy ...
- 使用JSONObject生成和解析json
1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组,以"[]"括起来 Object 对象,类似于C中的结构体 ...
- json:JSONObject与JSONArray的使用
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- java 后台封装json数据学习总结(一)
一.数据封装 1. List集合转换成json代码 List list = new ArrayList(); list.add( "first" ); list.add( &quo ...
随机推荐
- lsof 详解
lsof常用参数 lsof 如果不加任何参数,就会打开所有被打开的文件,建议加上一下参数来具体定位lsof -i 列出所有网络连接lsof -i tcp 列出所有tcp连接信息lsof -i udp ...
- django简单实现短url
一.短url的原理 什么是短url: 简单讲就是把普通正常访问的网址,转换成比较短的网址,例如:https://www.cnblogs.com/angelyan/articles/10667354.h ...
- PMP-49个过程快速导览
启动过程组 名正言顺 人鬼佛神 01 -4.1制定项目章程 -授权PM,启动项目 ● 编写一份正式批准项目并授权项目经理在项目活动中使用组织资源的文件的过程 ● 明确项目与组织战略目标之间的联系,确立 ...
- css玩转文字
<div style=" direction:rtl; unicode-bidi:bidi-override">文字被反转过来了</div> 执行后的效果为 ...
- 分享一套高级Java笔试题(实拍高清图)
分享一套高级Java笔试题 微信群里群友分享的 刚好他在笔试 有些问题不会发到群里求助 如果你最近正好在面试 需要参考需要提升 这套试题或许对你有用 下面是部分分享原图 下面是微信群中群友的热议 非常 ...
- 本地 win7 与虚拟机Centos7 ping互通和Centos7 上网设置
VM 12 安装虚拟机我就不表示了 很简单网上找找 一 .虚拟机设置 1.修改使用了 VMWare 12 虚拟机,Oracle VM 用得有点晕 2.配置VM 的NET环境 3.在VMware虚拟机任 ...
- USACO2008 Patting Heads /// 筛数 oj24705
题目大意: N (1 < N < 100,000)头牛被编号为1-N,围坐成圈 每头牛都被画上数字Ai (1 ≤ Ai ≤ 1,000,000),可能重复 逐个起来拍打 其他身上的数字是 ...
- ES6 学习 -- Class继承
(1)如何继承Class可以通过extends关键字实现继承,如下:class Father { } class Child extends Father { }// 这里子类Child继承父类Fat ...
- 深度探索C++对象模型之第一章:关于对象之C++对象模型
一.C和C++对比: C语言的Point3d: 数据成员定义在结构体之内,存在一组各个以功能为导向的函数中,共同处理外部的数据. typedef struct point3d { float x; f ...
- js数学公式-曲线运动
---勾股定理 a*a + b*b =c*c ---三角函数 正弦 : sin 余弦 : cos 正切 : tan 余切 : cot 正弦定理 a/sinA = b/sinB =c/sinC = 2r ...