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. 字符串匹配dp+bitset,滚动数组优化——hdu5745(经典)

    bitset的经典优化,即把可行性01数组的转移代价降低 bitset的适用情况,当内层状态只和外层状态的上一个状态相关,并且内层状态的相关距离是一个固定的数,可用bitset,换言之,能用滚动数组是 ...

  2. NX二次开发-UFUN获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)UF_OBJ_ask_display_properties

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  3. 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren

    今天在做Vue的时候,子组件关闭的时候,报如下错误 报错:vue.esm.js?65d7:610 [Vue warn]: Avoid mutating a prop directly since th ...

  4. 剑指offer——16二进制中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 有可能引起死循环解法: 每次判断最右端是不是1[与 & 1即可],是就cnt++,然后右移一位,直到num为0,结束 ...

  5. kubeadm 安装k8s

    环境要求: 机器名 ip地址 cpu和内存要求 kubernetes-master 10.0.0.11 2c2g(关闭swap) kubernetes-node1 10.0.0.12 2c2g(关闭s ...

  6. 20140316 window live write 插件 推荐代码高亮插件 构造函数只能用初始化成员列表方式的例子

    1.window live write 插件:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html 2.推荐代码高亮插件:W ...

  7. 什么是css块级元素和内联元素

    CSS文档流与块级元素(block).内联元素(inline),文档流这个概念理解了它,一堆CSS布局的理论都 变得易于理解,并且体会到CSS这套设计的合理性所在. 文档流 将窗体自上而下分成一行行, ...

  8. python学习5—一些关于基本数据结构的练习题

    python学习5—一些关于基本数据结构的练习题 # 1. use _ to connect entries in a list # if there are no numbers in list l ...

  9. Qt5 linux下的配置

    对于用Qt开发图形界面,Qt会用到openGL的相关库文件和头文件.虽然绝大多数的linux发行版中都没有预置安装这些开发工具,但是要安装它们,也是非常简单的.用一行安装命令即可安装完毕. Debia ...

  10. 用PS制作APP的界面图片

    今天就教大家怎么做出这种厚度的地方还不是白色的,而是根据界面内容交相呼应的图案的APP界面展示图片. 以苹果5S的尺寸为例. 步骤: 1.新建一个画布尺寸为:640*1136,然后保存,命名如:5S效 ...