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 ...
随机推荐
- Go 转义字符
Go 转义字符 package main import "fmt" func main() { fmt.Printf("Hello\tWorld!") } 本文 ...
- css制作旋转风车(transform 篇)
做这个案例之前首先要大概了解CSS的transform的属性 transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 看看效果图 打开的时候自动旋转 ...
- 牛客多校第九场 J Symmetrical Painting 计算几何/扫描线
题意: 平面上有几个宽度相同的矩形区域被涂黑了,让你找到一条横线横截若干个矩形,把这些黑色部分抠下来一部分使得它们以这条横线为对称轴,求能抠下来的最大面积. 题解: 在随着对称轴上移的过程中,必然有一 ...
- 基于Netty的RPC架构学习笔记(一):NIO
文章目录 传统的socket分析 举个
- hexo next主题深度优化(八),微加速
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 通过不断地上网查资料,引用的js.css.图片 ...
- 6.4 Data Types
Table 6-1 lists the size, representation, and range of each scalar data type for the C28x compiler. ...
- 剑指offer——32从上到下打印二叉树
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 题解: 就是简单的层序遍历 class Solution { public: vector<int> PrintFro ...
- 本地 win7 与虚拟机Centos7 ping互通和Centos7 上网设置
VM 12 安装虚拟机我就不表示了 很简单网上找找 一 .虚拟机设置 1.修改使用了 VMWare 12 虚拟机,Oracle VM 用得有点晕 2.配置VM 的NET环境 3.在VMware虚拟机任 ...
- <Django>博客项目
0.项目的通用流程 项目立项 需求分析 原型 前端 页面设计 UI及交互实现 后端 架构设计 数据库设计 代码模板实现 单元测试 网站整合 功能及集成测试 网站发布 1.BBS项目需求分析 需要哪些表 ...
- Shiro学习笔记1 —— Hello World
1.创建一个Maven工程加载Shiro的jar包 <!-- junit --> <dependency> <groupId>junit</groupId&g ...