Map (就一个json.jar)
public static void main(String[] args) {
List<Map<Integer, String>> m = new ArrayList<Map<Integer,String>>();
Map<Integer, String> i = new HashMap<Integer, String>();// {1=1, 111=111, 1111=1111, 11=11}
i.put(, "");
i.put(, "");
i.put(, "");
i.put(, "");
Map<Integer, String> ii = new HashMap<Integer, String>();// {1=1, 111=111, 1111=1111, 11=11}
ii.put(, "");
ii.put(, "");
ii.put(, "");
ii.put(, "");
m.add(i);
m.add(ii);// [{1=1, 111=111, 1111=1111, 11=11}, {1=1, 111=111, 1111=1111, 11=11}]
}
从外到里看,例子中就是一个数组,数组里面是两个json格式的字符串。这样分析思路就清晰多了。
工作中需要取出name4的值,你们会怎么取呢?。最初我都想过字符串截取,那时还不了解JSONArray,现在知道了,取出来也就相当容易了。
取出name4值过程步骤:1,将以上字符串转换为JSONArray对象;2,取出对象的第一项,JSONObject对象;3,取出name1的值
JSONObject对象;4,取出name2的值JSONObject对象;5,取出name4的值value2。
示例中json数组格式的字符串可以通过方法直接转换为JSONArray的格式:JSONArray.fromObject(String)
- </pre><pre name="code" class="java">JSONArray getJsonArray=JSONArray.fromObject(arrayStr);//将结果转换成JSONArray对象的形式
- JSONObject getJsonObj = getJsonArray.getJSONObject(0);//获取json数组中的第一项
- String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");
好了我们说说这两个对象。
1,JSONObject
json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}
2,JSONArray
json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的
Json对象中添加的是键值对,JSONArray中添加的是Json对象
- JSONObject Json = new JSONObject();
- JSONArray JsonArray = new JSONArray();
- Json.put("key", "value");//JSONObject对象中添加键值对
- JsonArray.add(Json);//将JSONObject对象添加到Json数组中
3,JSONObject与Map
Map map和json都是键值对,不同的是map中键值对中间用等号分开,json中键值对中间用冒号分开。其实json就是一种特殊形式的map。
import java.util.Iterator; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; public class map {
public static void main(String[] args) throws JSONException { // str是JSONObject
String str = "{\"people\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\"},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\"},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"}]}";
JSONObject jo = new JSONObject(str);// {"people":[{"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"},{"lastName":"Hunter","email":"bbbb","firstName":"Jason"},{"lastName":"Harold","email":"cccc","firstName":"Elliotte"}]}
// getJSONArray()表示返回值是JSONArray
JSONArray info = jo.getJSONArray("people");// [{"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"},{"lastName":"Hunter","email":"bbbb","firstName":"Jason"},{"lastName":"Harold","email":"cccc","firstName":"Elliotte"}]
System.out.println(info);
for (int i = ; i < info.length(); i++) {
JSONObject obj = info.getJSONObject(i);// {"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"}
Iterator it = obj.keys();
while (it.hasNext()) {
String key = (String) it.next();// lastName
System.out.println(obj.get(key));// McLaughlin
}
} // jsonContent是JSONObject
String jsonContent = "{'hello':'world','abc':'xyz'}";
JSONObject jsonObject = new JSONObject(jsonContent);// {"hello":"world","abc":"xyz"}
String str1 = jsonObject.getString("hello");// world
String str2 = jsonObject.getString("abc");// xyz
System.out.println(str1);
System.out.println(str2); // jsonContent是JSONArray
jsonContent = "[{'hello':333,'abc':false,'xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':true,'xyz':{'a':2,'b':'ba'}}]";
JSONArray jsonArray = new JSONArray(jsonContent);// [{"hello":333,"abc":false,"xyz":{"b":"ab","a":1}},{"hello":555,"abc":true,"xyz":{"b":"ba","a":2}}]
for (int i = ; i < jsonArray.length(); i++) {
JSONObject jsonobject2 = jsonArray.getJSONObject(i);// {"hello":333,"abc":false,"xyz":{"b":"ab","a":1}}
int value1 = jsonobject2.getInt("hello");//
boolean value2 = jsonobject2.getBoolean("abc");// false
// String value3=jsonobject2.getString("xyz");
JSONObject jsonobject3 = jsonobject2.getJSONObject("xyz");// {"b":"ab","a":1}
int value4 = jsonobject3.getInt("a");//
String value5 = jsonobject3.getString("b");// ab
System.out.println(value1);
System.out.println(value2);
System.out.println(value4);
System.out.println(value5);
} // str是JSONObject,没有最外层key的json
str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}],"
+ "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";
JSONArray newArray = new JSONArray();
JSONObject newJson = new JSONObject();
try {
JSONObject obj = new JSONObject(str); // {"AB":[{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}],"TI":[{"count":10,"value":"aa1"},{"count":15,"value":"aa2"},{"count":20,"value":"aa3"}]}
Iterator it = obj.keys();
while (it.hasNext()) {
String key = (String) it.next(); // AB
String value = obj.getString(key); // [{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}]
JSONArray array = obj.getJSONArray(key); // [{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}]
for (int i = ; i < array.length(); i++) {
JSONObject jsonobject = array.getJSONObject(i); // {"count":110,"value":"ab"}
jsonobject.put("name", key);
jsonobject.put("exp",
key + "=" + jsonobject.getString("value"));
newArray.put(jsonobject); // {"exp":"AB=ab","count":110,"name":"AB","value":"ab"}
}
}
newJson.put("groups", newArray);
System.out.println(newJson); // {"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]} } catch (JSONException e) {
e.printStackTrace();
}
} }
Map (就一个json.jar)的更多相关文章
- java中Array/List/Map/Object与Json互相转换详解(转载)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- 第3章 springboot接口返回json 3-1 SpringBoot构造并返回一个json对象
数据的使用主要还是以JSON为主,我们不会去使用XML. 这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller. @Respons ...
- java中Array/List/Map/Object与Json互相转换详解
http://blog.csdn.net/xiaomu709421487/article/details/51456705 JSON(JavaScript Object Notation): 是一种轻 ...
- 一起写一个JSON解析器
[本篇博文会介绍JSON解析的原理与实现,并一步一步写出来一个简单但实用的JSON解析器,项目地址:SimpleJSON.希望通过这篇博文,能让我们以后与JSON打交道时更加得心应手.由于个人水平有限 ...
- Spring初始化 Map 和 解析Json value
单独定义Map数据结构的bean: <bean id= "expToLevelMap" class="org.springframework.beans.facto ...
- 如何编写一个JSON解析器
编写一个JSON解析器实际上就是一个函数,它的输入是一个表示JSON的字符串,输出是结构化的对应到语言本身的数据结构. 和XML相比,JSON本身结构非常简单,并且仅有几种数据类型,以Java为例,对 ...
- 《Python网络编程》学习笔记--使用谷歌地理编码API获取一个JSON文档
Foundations of Python Network Programing,Third Edition <python网络编程>,本书中的代码可在Github上搜索fopnp下载 本 ...
- 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}]
// 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}] // Object.assign方法的第一个参数是目标对象,后面的参数都是源对象. var list ...
- BeanShell使用json.jar包处理Json数据
环境准备 ①Jmeter版本 ,JDK ②前置条件:将json.jar包置于..\lib\下, 如果还是报错,可以将该jar包添加到测试计划的Library中:否则会报:Typed variable ...
随机推荐
- css要点
1.对inline-block设置overflow: hidden会造成baseline移动,因此需要设置vertical-align才不会出现样式问题. 2.使用flex时,需要对设置flex: 1 ...
- 腾讯新闻多图jQuery相册展示效果代码
腾讯新闻多图jQuery相册代码,带左右切换箭头,带缩略图,可左右切换,点击缩略图展示原图. 在线演示本地下载
- Android 自定义控件之圆形扩散View(DiffuseView)
实现效果 使用 属性方法 代码 源码下载 参考链接 实现效果 使用 XML中: <com.airsaid.diffuseview.widget.DiffuseView android:id=&q ...
- 10) 十分钟学会android--app数据保存三种方式
虽然可以在onPause()时保存一些信息以免用户的使用进度被丢失,但大多数Android app仍然是需执行保存数据的动作.大多数较好的apps都需要保存用户的设置信息,而且有一些apps必须维护大 ...
- RGB_D_开发征程(使用Kinect)
学习历程依此为纲! Kinect学习资料: kinect和openNI开发资料汇总:http://blog.csdn.net/chenli2010/article/details/6887646 原始 ...
- 解决strip: Unable to recognise the format of the input file问题
前言 在编译xilinx的uboot的时候出现了一个问题,始终报错:“strip: Unable to recognise the format of the input file `gen_et ...
- 分层利器 facade
Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用.
- MySQL_pymysql模块
安装:pip install pymysql 基本操作 import pymysql conn=pymysql.connect(host=',database='lary',charset='utf8 ...
- LSTM比较RNN
LSTM只能避免RNN的梯度消失(gradient vanishing),但是不能对抗梯度爆炸问题(Exploding Gradient). 梯度膨胀(gradient explosion)不是个严重 ...
- MAC上ITERM2配置SZ, RZ远程上传和下载文件
MAC上ITERM2配置SZ, RZ远程上传和下载文件 From article 在Windows上的SecureCRT.XShell远程连接Linux服务器,通常可以使用sz.rz等命令来上传和下载 ...