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. 暑假集训test-8-30

    这套题有毒,T1标程挂了,T2题面完全莫名其妙,T3没有告诉取模害我打了好久高精... A题. 统计每个数后面比它小的数的个数记作f把,操作一个数就是把它后面所有比它小的数和它的f清0,然后若是它到它 ...

  2. NX二次开发-通过点击按钮来控制显示工具条

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...

  3. java 8 lambda函数

    1 为什么要引进lambda函数 可以简化编码,将事情更多的交给编译器,让编译器帮我们推断我们写的代码的完整形式. 2 lambda函数的语法 2.1 -> (arg1, arg2) -> ...

  4. java 读取mysql中数据 并取出

    public static String url = null; public static String username = null; public static String password ...

  5. PyQt5 安装及简单实例 -- 标签设置

    - 1 -    PyQt5安装,鉴于pip工具,安装其实很简单, 如下:(最好将pip索引配置成国内镜像,速度比默认的快得多) pip3 isntall PyQt5 - 2 -    利用Pycha ...

  6. Android 开发 框架系列 OkHttp文件下载功能实现(含断点续传)

    前言 此篇博客只是下载功能的记录demo,如果你还不太了解okhttp可以参考我的另一篇博客https://www.cnblogs.com/guanxinjing/p/9708575.html 代码部 ...

  7. apache 80 端口 反向代理 tomcat 8080端口

    最近有个jsp的项目要放到服务上,但服务器上已经有了XAMPP(apache + mysql + php), 已占用了80端口.但http默认是访问80端口的. 先把tomcat 环境搭建起来, 发现 ...

  8. 阿里云ECS发送邮件失败

    阿里云发送SMTP邮件失败   N多测试发现 阿里云服务器不能用25端口发邮件,配置465端口阿里云发送邮件是成功的 修改mail.rc 里的smtp 端口为465 配置如下 set from=*** ...

  9. 廖雪峰Java15JDBC编程-3JDBC接口-5JDBC连接池

    1. JDBC连接池 1.1 JDBC连接池简介 线程池可以复用一个线程,这样大量的小任务通过线程池的线程执行,就可以避免反复创建线程带来的开销. 同样JDBC可以复用一个JDBC连接 JDBC的连接 ...

  10. redis和ehcache的区别,存储方式(各属于内存还是外存)

    ehcache属于内存存储,redis的缓存属于内存存储,redis的持久文件属于外存存储: redis是通过socket访问到缓存服务,效率比ecache低,比数据库要快很多,处理集群和分布式缓存方 ...