JSON is a lightweight data exchange format that uses two structures, objects and arrays, to represent data. Objects are used to represent key-value pairs, while arrays are used to represent ordered lists of values. Therefore, the data format of JSON is actually a composite structure of objects and arrays. This simple and flexible format makes JSON a widely used choice for data interaction and storage.
Your example JSON data is a name/value pair, in which there is only one variable name of "wegInstLt", and the value is an array containing one entry. Assume var info={"wegInstLt":[{"billTypeCode":"0","channelId":530,"channelKind":"03","isHaveBillDate":"0","isHavePayAmount":"0"," isLeePay":"0","isPrePay":"1","wiliCode":"1238","wiliItem":"00","wiliName":"Harbin Water Company","wltiId":10345}]} ; To get "Harbin Water Company" you can write like this:
info.wegInstLt[0].wiliName
I hope this helps, and I wish you a happy life!
org.json.JSONObject
You can use this package to achieve your needs. Search this package on the Internet. It contains some methods that can convert a JSON string into a JSONObject object and vice versa. You can check its API documentation for specific details.
How about your string?
public static void main(String[] args) throws JSONException {
String sJson = "[{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'1','spsl':'2'},{'gwcxxid':' 3','spsl':'4'}]";
JSONArray jsonArray = new JSONArray(sJson);
int iSize = jsonArray.length();
System.out.println("Size:" iSize);
for (int i = 0; i
JSONObject jsonObj = jsonArray.getJSONObject(i);
System.out.println("[" i "]gwcxxid=" jsonObj.get("gwcxxid"));
System.out.println("[" i "]spsl=" jsonObj.get("spsl"));
System.out.println();
}
}
Use native parsing:
String json = "...";
//Traverse the values in the array to get each independent object, then get the corresponding value and set it to the declared object. Finally, after the object is created, it is added to the collection, such as the fragment in my own code:
for (int j = 0; j
obj = array.getJSONObject(j);
Data data = new Data();
mDataList.add(data);
}
Array declaration
In the declaration format of the array, "data type" is the data type of the declared array element, which can be any data type in the Java language, including simple types and structural types. "Array name" is a name used to unify these same data types, and its naming rules are the same as those of variables.
After the array is declared, the next step is to allocate the memory required for the array. At this time, the operator new must be used. The "number" tells the compiler how many elements the declared array should store, so the new operator The symbol tells the compiler to allocate a space in memory for the array according to the number in the brackets. The method of allocating memory space for array elements using the new operator is called dynamic allocation.
Reference for the above content: Encyclopedia-Array
The above is the detailed content of Understand the basic structure of the JSON data format. For more information, please follow other related articles on the PHP Chinese website!