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的更多相关文章

  1. JSONObject

    JAR包简介: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.j ...

  2. json---简单入门

    1.推荐使用第三种方式JAVABEAN的方式(使用前引入org.json) package day05; import java.util.HashMap; import java.util.Map; ...

  3. 日志——JSON的相关方法

    http://www.cnblogs.com/henryxu/archive/2013/03/10/2952738.html JSON  jar包: commons-lang.jar commons- ...

  4. jsonObject jsonarray

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  5. JSONObject与JSONArray的使用

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  6. [转]JSONObject与JSONArray的使用

    http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html 参考文献: http://blog.csdn.net/huangwuy ...

  7. 使用JSONObject生成和解析json

    1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组,以"[]"括起来 Object 对象,类似于C中的结构体 ...

  8. json:JSONObject与JSONArray的使用

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  9. java 后台封装json数据学习总结(一)

    一.数据封装 1. List集合转换成json代码 List list = new ArrayList(); list.add( "first" ); list.add( &quo ...

随机推荐

  1. 同构图+思维构造——牛客多校第六场E

    考的其实是同构图的性质: 1.同构图的顶点数,边数相等 2.同构图通过点的映射后邻接矩阵相同 这篇博客讲的很好https://www.jianshu.com/p/c33b5d1b4cd9 本题还需要一 ...

  2. 使用electron-vue搭建桌面应用程序项目

    vue-cli+electron一种新的脚手架(vue-electron)vue-electron主要业务逻辑都放在src下的renderer文件夹内,和之前的vue-cli搭建项目流程没有任何区别 ...

  3. NX二次开发-创建图纸尺寸表达式抑制UF_DRF_add_controlling_exp

    #include <uf.h> #include <uf_modl.h> #include <uf_drf.h> #include <uf_obj.h> ...

  4. VMware Workstation 添加磁盘 挂载目录(centos)

    参考文档: Linux命令大全 需求:测试环境虚拟机某个目录下空间不足,准备通过添加一块磁盘,并将该目录挂载到该磁盘 前面几步在测试服务器上操作的,截图困难,所以网上找了几张设置图 关闭虚拟机(没测试 ...

  5. mysql安装总结

    #mysql安装# 安装工具:yum -y install gcc-c++ ncurses cmake make ncurses-devel # 拷贝配置文件到指定目录:rm -rf /etc/my. ...

  6. Area in Triangle /// oj10229

    题目大意: 给出三角形的三个顶点 再给一条绳(绳长不超过三角形周长) 求绳子在三角形中能围出的最大面积 题解链接 http://blog.sina.com.cn/s/blog_6a46cc3f0100 ...

  7. PKPM快捷键

    e删除sc删除节点hq绘制直线墙lbz布置梁zz楼层组装bsc板生成bxg板修改门窗洞dbz(洞布置)全房间洞(fd)正交轴网zww拉伸s

  8. Vue键盘事件

    键盘事件一 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  9. java内存模型和垃圾回收

    摘抄并用于自查 JVM内存模型 1. Java程序具体执行的过程: Java源代码文件(.java后缀)会被Java编译器编译为字节码文件(.class后缀) 由JVM中的类加载器加载各个类的字节码文 ...

  10. 解决vs code 内置终端,字体间隔过大问题。(linux centos7 ubuntu成功)

    去文件-首选项-设置里修改. "terminal.integrated.fontFamily": ""注意此处默认为空白,所以显示的就比较奇怪. 此处我改为&q ...