Parsing data in JSON form on Android
Last Update:2015-05-31
Source: Internet
Author: User
<span id="Label3"></p>1, JSON (JavaScript Object Notation) definition:<p><p>A lightweight data interchange format with good readability and easy-to-write features. The Industry's mainstream technology provides a complete solution (somewhat like regular expressions, which is supported in most languages today), enabling data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language System.</p></p>2, the structure of json:<p><p>(1) name/value Pairs (unordered): similar to the familiar keyed list, Hash table, disctionary, and associative array. There is another class "Bundle" in the Android platform that has similar behavior in some way.</p></p><p><p>(2) Array (ordered): A set of ordered data lists, processing This class is step-by, see in detail the implementation in Jsonparser.java.</p></p>JSON object<p><p>The Name/value object is an unordered collection of Pairs. {name:value, name:value, name:value ...}, example: {"name": "jack", "age": 20}</p></p>JSON Arrays (array)<p><p>An array is an ordered collection of values (value). [value, value, Value ...] The value (value) can be a <span style="background-color: #ffff99;">string enclosed in double quotation marks</span> (string), A numeric value (number), true, false, null, an object, or an array. These structures can be nested. A string is a collection of any number of Unicode characters surrounded by double quotation marks, <span style="background-color: #ffff99;">escaped with a backslash</span> . A character (character) is a separate string (character string).</p></p><p><p>At the time of the test, the following string is Shown:</p></p><pre><pre><span style="color: #0000ff;">String result = "{\" name\ ": \" jack\ ", \" age\ ": 20}";</span></pre></pre><p><p>Example 1: <span class="diigoHighlight id_5679ff34c0775face41c175e7f7ff865 type_0 green">array contains objects (object)</span></p></p><p><p>[{"id": 1, "name": "jack", "age": $}, {"id": 2, "name": "tom", "age": 23}, ...]</p></p><p><p>Example 2: the same object can contain an array</p></p><p><p>(1) An object contains 1 arrays, 2 sub-objects</p></p><p><p>{"root": [{"id": "001", "name": "jack"},{"id": "002", "name": "tom"},{"id": "003", "name": "helen"}],<br>"total": 3,<br>"success": true<br>}</p></p><p><p>(2) you can also object nested child objects, sub-objects nested arrays</p></p><p><p>{"calendar":<br>{"calendarlist":<br>[<br>{"id": "001", "name": "jack"},<br>{"id": "002", "name": "tom"}<br>]<br>}<br>}</p></p><p><p>Summary sentence: <span style="background-color: #ffff99;">The format is varied and can be nested with each other</span> .</p></p><p><p>--------------------------------------------------------------------------------------------</p></p>Contains four json-related classes and a exceptions in android:<p><p>jsonarray, jsonobject, jsonstringer, jsontokener, jsonexception, In addition to the first two, the other in the analysis is not long use.</p></p>(1) jsonobject:<p><p>This is the basic unit in the system for JSON definition, which contains a pair of Key/value Values.</p></p><p><p>Its response to the external (External: apply the ToString () method output Value) is reflected as a standard string (for example: {"JSON": "Hello, world"}, enclosing the outer brace with the key and value separated by the colon ":"). Its action format for internal (Internal) behavior is slightly, for example: initializing a jsonobject instance, <span class="diigoHighlight id_e5a20d555b4a997a97d6271f9e0dc62a type_0 green">referencing the internal put () method to add a value <span class="diigoHighlightCommentLocator">: New Jsonobject (). put ("JSON", "Hello, world" ! "), <span class="diigoHighlight id_f91a83080b9bcadb0592a23610942749 type_0 green">separated by a comma", "between key and value <span class="diigoHighlightCommentLocator">. </span></span></span></span></p></p><p><p>The types of value include: Boolean, jsonarray, jsonobject, number, string, or the default value Jsonobject.null Object.</p></p><p><p>There are two different methods of fetching values:</p></p><p><p><span class="diigoHighlight id_0750a9291147d98b240c7be3763059a7 type_0 green">Get ():<span class="diigoHighlightCommentLocator"> used when determining the existence of a value, or a exception message will be thrown when the relevant key cannot be retrieved. </span></span></p></p><p><p><span class="diigoHighlight id_d5a8890cd65bc9fd9fa10232e9a03a98 type_0 green">Opt ():<span class="diigoHighlightCommentLocator"> This method is relatively flexible and returns a default value when the specified value cannot be obtained, and does not throw an Exception. </span></span></p></p>(2) jsonarray:<p><p>It represents an ordered set of Values. Converting it to a string output (toString) is performed in the form of a square bracket, separated by a comma "," (for example: [value1,value2,value3], You can personally use the short code to understand its format more intuitively). The inside of this class also has query behavior, both get () and opt () can return the specified value through the index index, and the put () method is used to add or replace Values.</p></p><p><p>similarly, The value type of this class can include: Boolean, jsonarray, jsonobject, number, string, or default value Jsonobject.null Object.</p></p>(3) Jsonstringer:<p><p>According to the official explanation, this class can help create jsontext quickly and easily. Its greatest advantage is that it can reduce program exceptions due to format errors, which can automatically create JSON text in strict accordance with JSON syntax rules (syntaxrules). Each Jsonstringer entity can only create one JSON Text.</p></p><p><p>Follow the example below to learn about other relevant information: The result is a standard set of JSON text:{"name": "pig"}, where The. object () and. EndObject () must be used together in order to add a boundary to the value according to the object Standard. similarly, there is a standard set of methods for arrays to generate Boundaries. array () and. Endarray ().</p></p>(4) Jsontokener:<p><p>This is the class for the system to parse the JSON source string for the Jsonobject and Jsonarray constructors, which can extract numeric information from the source String.</p></p><p><p>Jsonexception: is the exception information thrown by the json.org class.</p></p><p><p>Practiced hand simple parsing of several common types, first write a class, implement a static method to call Jsonparser.java:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">ujs.jsonparser;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.ArrayList;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.HashMap;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.List;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Map;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.json.JSONArray;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.json.JSONException;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.json.JSONObject;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonparser {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* JSON array inline JSON object * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">string *, Data form string result = * "[{\" id\ ": 1,\" name\ ": \" piglet \ ", \" age\ ": 2},{\" id\ ": 2,\" Nam e\ ": \" puppy \ ", \" age\ ": 3}]" *; * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">Exception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>list<map<string, string>><span style="color: #000000;"><span style="color: #000000;">getjsonarray (string String) {List</span></span><map<string, string>> list =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>arraylist<map<string, string>><span style="color: #000000;"><span style="color: #000000;">(); Map</span></span><string, string> map =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; Jsonarray jsonarray; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{jsonarray</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonarray (string); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < Jsonarray.length (); i++<span style="color: #000000;"><span style="color: #000000;">) {jsonobject Item</span></span>=<span style="color: #000000;"><span style="color: #000000;">Jsonarray.getjsonobject (i); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>id = item.getint ("id"<span style="color: #000000;"><span style="color: #000000;">); String name</span></span>= Item.getstring ("name"<span style="color: #000000;"><span style="color: #000000;">); String</span> age</span>= Item.getstring ("age"<span style="color: #000000;"><span style="color: #000000;">); Map</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>hashmap<string, string><span style="color: #000000;"><span style="color: #000000;">(); Map.put (</span></span>"id", ID + ""<span style="color: #000000;"><span style="color: #000000;">); Map.put (</span></span>"name"<span style="color: #000000;"><span style="color: #000000;">, name); Map.put (</span></span>"age"<span style="color: #000000;"><span style="color: #000000;">, age); List.add (map); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(jsonexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Test Print information, You can delete</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(map<string, string><span style="color: #000000;"><span style="color: #000000;">Map2:list) {String ID</span></span>= Map2.get ("id"<span style="color: #000000;"><span style="color: #000000;">); String name</span></span>= Map2.get ("name"<span style="color: #000000;"><span style="color: #000000;">); String</span> age</span>= Map2.get ("age"<span style="color: #000000;"><span style="color: #000000;">); System.out.println (</span></span>"id:" + ID + "name:" + name + "age:" +<span style="color: #000000;">age <span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">list; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* JSON object embedded JSON array (array inline JSON Object) * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">String * For example: string result = * "{\" total\ ": 2,\" success\ ": true,\" arraydata\ ": [{\" id\ ": 1,\ "name\": \ "piglet \"},{\ "id\": 2,\ "name\": \ "kitten \"}]} "*; * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">Exception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>list<map<string, string>><span style="color: #000000;"><span style="color: #000000;">getjsonobject (string String) {List</span></span><map<string, string>> list =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>arraylist<map<string, string>><span style="color: #000000;"><span style="color: #000000;">(); Map</span></span><string, string> map =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; Jsonobject jsonobject; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;">total <span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{jsonobject</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonobject (string); </span>total</span>= Jsonobject.getint ("total"<span style="color: #000000;"><span style="color: #000000;">); Boolean Success</span></span>= Jsonobject.getboolean ("success"<span style="color: #000000;"><span style="color: #000000;">); System.out.println (</span></span>"total:" + all + "| Success: "+ success);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Test data, information has been obtained</span></span>Jsonarray Jsonarray = Jsonobject.getjsonarray ("arraydata");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the array inside the object</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < Jsonarray.length (); i++<span style="color: #000000;"><span style="color: #000000;">) {jsonobject Item</span></span>=<span style="color: #000000;"><span style="color: #000000;">Jsonarray.getjsonobject (i); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>id = item.getint ("id");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the value corresponding to the object</span></span>String name = item.getstring ("name"<span style="color: #000000;"><span style="color: #000000;">); Map</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>hashmap<string, string> ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Store in Map</span></span>Map.put ("id", ID + "" "<span style="color: #000000;"><span style="color: #000000;">); Map.put (</span></span>"name"<span style="color: #000000;"><span style="color: #000000;">, name); List.add (map); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(jsonexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Test data, can be deleted</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(map<string, string><span style="color: #000000;"><span style="color: #000000;">Map2:list) {String ID</span></span>= Map2.get ("id"<span style="color: #000000;"><span style="color: #000000;">); String name</span></span>= Map2.get ("name"<span style="color: #000000;"><span style="color: #000000;">); System.out.println (</span></span>"id:" + ID + "| Name: "+<span style="color: #000000;"><span style="color: #000000;">name); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">list; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;"> * * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">String * String result = * "{\" name\ ": \" piglet \ ", \" age\ ": all, \" content\ ": {\" questionstotal \ ": 2,\" questions\ ": [{\" question\ ": \" What ' s your name?\ ", \" answer\ ": \" piggy \ "},{\" question\ ": \" What ' s your age\ ", \" ans Wer\ ": \" 23\ "}]}}" *; * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">Exception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>list<map<string, string>><span style="color: #000000;"><span style="color: #000000;">Getjson (string String) {List</span></span><map<string, string>> list =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>arraylist<map<string, string>><span style="color: #000000;"><span style="color: #000000;">(); Map</span></span><string, string> map =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; Jsonobject jsonobject; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{jsonobject</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonobject (string); String name</span></span>= Jsonobject.getstring ("name"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Age = Jsonobject.getint ("age"<span style="color: #000000;"><span style="color: #000000;">); System.out.println (</span></span>"name:" + name + "| Age: "+ age");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">test Data</span></span>Jsonobject Contentobject = Jsonobject.getjsonobject ("content"<span style="color: #000000;"><span style="color: #000000;">); String Questionstotal</span></span>= Contentobject.getstring ("questionstotal");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets a value from the object</span></span>System.out.println ("questionstotal:" + questionstotal);<span style="color: #008000;"><span style="color: #008000;">//</span></span>Jsonarray Contentarray = Contentobject.getjsonarray ("questions"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < Contentarray.length (); i++<span style="color: #000000;"><span style="color: #000000;">) {jsonobject Item</span></span>= Contentarray.getjsonobject (i);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get each object</span></span>String question = item.getstring ("question");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the value corresponding to the object</span></span>String answer = item.getstring ("answer"<span style="color: #000000;"><span style="color: #000000;">); Map</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>hashmap<string, string> ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Store in Map</span></span>Map.put ("question"<span style="color: #000000;"><span style="color: #000000;">, question); Map.put (</span></span>"answer"<span style="color: #000000;"><span style="color: #000000;">, answer); List.add (map); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(jsonexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Test data, can be deleted</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(map<string, string><span style="color: #000000;"><span style="color: #000000;">Map2:list) {String question</span></span>= Map2.get ("question"<span style="color: #000000;"><span style="color: #000000;">); String Answer</span></span>= Map2.get ("answer"<span style="color: #000000;"><span style="color: #000000;">); System.out.println (</span></span>"question:" + question + "| Answer: "+<span style="color: #000000;"><span style="color: #000000;">answer); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">list; }}</span></span></pre><p><p>Here is a simple call:</p></p><pre><pre>String result = "[{\" id\ ": 1,\" name\ ": \" piglet \ "," age\ ": 2},{\" id\ ": 2,\" name\ ": \" puppy \ ", \" age\ ": 3}]"<span style="color: #000000;">; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">String result = </span> <span style="color: #008000;">//</span> <span style="color: #008000;">"{\" total\ ": 2,\" success\ ": true,\" arraydata\ ": [{\" id\ ": 1,\" name\ ": \" piggy \ "},{\" id\ ": 2,\" name\ ": \" kitten \ "}]}"; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">String result = </span> <span style="color: #008000;">//</span> <span style="color: #008000;">"{\" name\ ": \" piglet \ ", \" age\ ":, \" content\ ": {\" questionstotal\ ": 2,\" questions\ ": [{\" question\ ": \" What ' s your Name?\ ", \" answer\ ": \" piglet \ "},{\" question\ ": \" What ' s your age\ ", \" answer\ ": \" 23\ "}]}";</span> Jsonparser.getjsonarray (result);</pre></pre><p><p></p></p><p><p>Parsing data in JSON form on Android</p></p></span>