How to construct and parse JSON data under J2ME
JSON data in J2ME under the structure and parsing is what, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need to learn, I hope you can harvest.
Here we describe the construction and parsing of JSON data under J2ME, mainly including creating JSON data pairs and parsing JSON key pairs.
Construction and Parsing of JSON Data under J2ME
JSON provides plug-in packages for use in J2ME environments. The download address is www.JSON.org/java/org.JSON.me.zip
This article briefly explains how JSON can be used in a J2ME environment.
1. Create JSON data pairs
1) Create a single key-value pair wrapper: use JSONObject under org.JSON.me
This class provides various put(**,**) functions, which can be called directly.
eg:JSONObjectJSONObject=newJSONObject(); JSONObject.put("name","");
This creates a simple JSON data pair {"name":"drop without stem"}
2) Create array key-value pair encapsulation: use JSONArray under org.JSON.me
Unlike JSONObject, JSONArray provides the function publicJSONArray put (intindex,Vectorvalue)throwsJSONException to create arrays directly.
In fact, if you need to convert the model class to JSON data in j2me, you can generally define your own function in the model class.
publicStringtoJSONString(){}
2. Parse JSON key-value pairs
1)Parse general JSON key-value pairs (the parsed object must start with "{", otherwise an error will be reported)
Firstly, the parsed object is constructed
Suppose that the parsed object is:
Strings={"age":"16","name":"fall without stalk","gender":"male"}; JSONObjectJSONObject=newJSONObject(s);
Once constructed, use JSONObject.get("age") to get 16, the same for all other values.
2)Parse array key-value pairs (parsed object must start with "]", otherwise an error will be reported)
Suppose that the parsed object is:
Strings=[{"age":"16","name":"samyou","gender":"male"}, {"age":"18","name":"ygy","gender":"female"}]; JSONArray =newJSONArray(s);//two sequences intlength=JSONArray.length(); for(inti=0;i { JSONObjectJSONObject=JSONArray.getJSONObject(i); /* Custom Processing Statements.................................................. * Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.