tags: JAVA JSON JSONObject JSONArray
package com.yunos.tv.video.resource.controller.web;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
/ / The difference between JsonObject and JsonArray is that JsonObject is an object form, JsonArray is an array form
/ / Create the first method of JsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("UserName", "ZHULI");
jsonObject.put("age", "30");
jsonObject.put("workIn", "ALI");
System.out.println("jsonObject1:" + jsonObject);
/ / Create a second method of JsonObject
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("UserName", "ZHULI");
hashMap.put("age", "30");
hashMap.put("workIn", "ALI");
System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
/ / Create a JsonArray method 1
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "ZHULI");
jsonArray.add(1, "30");
jsonArray.add(2, "ALI");
System.out.println("jsonArray1:" + jsonArray);
/ / Create JsonArray method 2
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("ZHULI");
arrayList.add("30");
arrayList.add("ALI");
System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
/ / If the JSONArray parses a HashMap, it will put the entire object into the value of an array
System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
/ / Assemble a complex JSONArray
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("UserName", "ZHULI");
jsonObject2.put("age", "30");
jsonObject2.put("workIn", "ALI");
jsonObject2.element("Array", arrayList);
System.out.println("jsonObject2:" + jsonObject2);
}
}
result:
jsonObject1:{“UserName”:“ZHULI”,“age”:“30”,“workIn”:“ALI”}
jsonObject2:{“workIn”:“ALI”,“age”:“30”,“UserName”:“ZHULI”}
jsonArray1:[“ZHULI”,“30”,“ALI”]
jsonArray2:[“ZHULI”,“30”,“ALI”]
jsonArray FROM HASHMAP:[{“workIn”:“ALI”,“age”:“30”,“UserName”:“ZHULI”}]
jsonObject2:{“UserName”:“ZHULI”,“age”:“30”,“workIn”:“ALI”,“Array”:[“ZHULI”,“30”,“ALI”]}
Parse the JSON string:
package com.yunos.tv.video.resource.controller.web;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
String jsonString = "{\"UserName\":\"ZHULI\",\"age\":\"30\",\"workIn\":\"ALI\",\"Array\":[\"ZHULI\",\"30\",\"ALI\"]}";
/ / Convert the Json string to a java object
JSONObject obj = JSONObject.fromObject(jsonString);
/ / Get the UserName in the Object
if (obj.has("UserName")) {
System.out.println("UserName:" + obj.getString("UserName"));
}
/ / Get ArrayObject
if (obj.has("Array")) {
JSONArray transitListArray = obj.getJSONArray("Array");
for (int i = 0; i < transitListArray.size(); i++) {
System.out.print("Array:" + transitListArray.getString(i) + " ");
}
}
}
}
return:
UserName:ZHULI
Array:ZHULI Array:30 Array:ALI
A brief introduction to optString:
.getString(String name), like optString(String name), returns a field in object. Just when there is no return value, getString(String name) throws an error, optString(String name) returns a null value.
A brief introduction to jsonArray.opt:
returns a corresponding JSONObject object when traversing a jsonArray object
Mainly use Array to return the specified data format. The code sample is as follows: ...
JSONObject Everyone sees JSONOBJECT should really be related to JSON format, yes, JsonObject is a class that will be data JSON, which is a key-value form and MAP type compare. Let's take a look at thi...
Transfer from https://my.oschina.net/zimingforever/blog/62066 Below is a very imageive example found from the Internet, running locally. import net.sf.json.JSONArray; import net.sf.json.JSONObject; pu...
JsonObject and Jsonarray simple use Foreword It has always been used to install or respond data with an object. This is also a bad place. Benefits: The specific data of the request or response corresp...
json Is a key value corresponds to a simple one to one relationship. JSONObject json objectsIs a value corresponding to a key (Key-value pairs), Using braces {} such as: {key: value} JSONArray json ar...
JSONOBJCET traverses: Jsonarray traverses:...
JAVA analysis jasonObject Java analysis jsonarray...
In the process of learning Java, we will inevitably encounter to process JSON data formats, and now you will introduce JSON in Java. 1, handle JSONOBJECT 2, handle JSonarray Simple and easy to underst...
JsonObject to JSONARRAY JSONArray data = JSONArray.parseArray(reqMap.get("userListDto").toString()); JSONObject jsonObj = JSONObject.fromObject(resultMap.get("data")); JSONArray ce...
Unlike Java and PHP parsing JSON always produce a more painful process. But using JSONObject and JSONArray will make the whole process is relatively comfortable. The need to rely on packet: commons-la...