Android开源:JSON - 简单易用的json解析

IsabelLandi 7年前
   <h2>JSON</h2>    <p><img src="https://simg.open-open.com/show/f5a4a4912fd35f29eb553589653a6258.jpg"></p>    <p>简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储.</p>    <h2>添加到项目</h2>    <p>To use JSON you must add it as a dependency in your Gradle build:</p>    <p>Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:</p>    <pre>  allprojects {          repositories {              ...              maven { url "https://jitpack.io" }          }      }</pre>    <p>Step 2. Add the dependency</p>    <pre>  dependencies {      compile 'com.github.amirdew:JSON:v1.0.0'  }</pre>    <h2>使用</h2>    <p>You can create JSON object from string and access data with key() and index() methods.</p>    <pre>  String simpleJsonString = "{\"id\":1,\"name\":\"A green door\",\"price\":12.5,\"tags\":[\"home\",\"green\"]}";    JSON json = new JSON(simpleJsonString);    //access data  String firstTag = json.key("tags").index(0).stringValue();  Double price = json.key("price").doubleValue();</pre>    <p>products json:</p>    <pre>  [    {      "id": 2,      "name": "An ice sculpture",      "price": 12.50,      "tags": ["cold", "ice"],      "dimensions": {        "length": 7.0,        "width": 12.0,        "height": 9.5      },      "warehouseLocation": {        "latitude": -78.75,        "longitude": 20.4      }    },    {      "id": 3,      "name": "A blue mouse",      "price": 25.50,      "dimensions": {        "length": 3.1,        "width": 1.0,        "height": 1.0      },      "warehouseLocation": {        "latitude": 54.4,        "longitude": -32.7      }    }  ]</pre>    <p>loop:</p>    <pre>  for(int i=0; i<products.count(); i++){       json productInfo = products.index(i);     String productName = productInfo.key("name").stringValue();  }</pre>    <p>JSON is exception and null free, you can use key() and index() many times without worry about any exception.</p>    <pre>  int someValue = products.index(8).key("someKey").index(1).key("someOther").intValue();   //someValue = 0</pre>    <p>check index or key is exist or is null:</p>    <pre>  if( products.index(3).key("someKey").isNull() ){       /*...*/    }    if( products.index(1).key("someKey").exist() ){     /*...*/    }</pre>    <h2>方法</h2>    <table>     <thead>      <tr>       <th>Method</th>       <th>Input type</th>       <th>Return type</th>       <th>Default</th>       <th>Description</th>      </tr>     </thead>     <tbody>      <tr>       <td>key()</td>       <td>String</td>       <td>JSON</td>       <td>-</td>       <td>if object is a dictionary return JSON object with input key</td>      </tr>      <tr>       <td>index()</td>       <td>int</td>       <td>JSON</td>       <td>-</td>       <td>if object is a array return JSON object with input index</td>      </tr>      <tr>       <td>stringValue()</td>       <td>-</td>       <td>String</td>       <td>empty string ("")</td>       <td>return .toString() of object</td>      </tr>      <tr>       <td>intValue()</td>       <td>-</td>       <td>int</td>       <td>0</td>       <td>return integer value if possible</td>      </tr>      <tr>       <td>longValue()</td>       <td>-</td>       <td>long</td>       <td>0</td>       <td>return long value if possible</td>      </tr>      <tr>       <td>doubleValue()</td>       <td>-</td>       <td>Double</td>       <td>0</td>       <td>return Double value if possible</td>      </tr>      <tr>       <td>booleanValue()</td>       <td>-</td>       <td>boolean</td>       <td>false</td>       <td>return true if object is kind of boolean and true or is kind of string and equal "true"</td>      </tr>      <tr>       <td>value()</td>       <td>-</td>       <td>Object</td>       <td>-</td>       <td>return related object</td>      </tr>      <tr>       <td>count()</td>       <td>-</td>       <td>int</td>       <td>0</td>       <td>if related object is a dictionary return number of keys, if related object is a array return length of array</td>      </tr>      <tr>       <td>isNull()</td>       <td>-</td>       <td>boolean</td>       <td>-</td>       <td>return true if related object is null</td>      </tr>      <tr>       <td>exist()</td>       <td>-</td>       <td>boolean</td>       <td>-</td>       <td>return true if related object with index or key exists</td>      </tr>      <tr>       <td>getJsonArray()</td>       <td>-</td>       <td>JSONArray</td>       <td>null</td>       <td>return related JSONArray</td>      </tr>      <tr>       <td>getJsonObject()</td>       <td>-</td>       <td>JSONObject</td>       <td>null</td>       <td>return related JSONObject</td>      </tr>     </tbody>    </table>    <h2>用法 - 生成json,数据保存</h2>    <p>You can use JSON.dic() and JSON.array() static methods to generate json string or hold and pass data</p>    <pre>  JSON generatedJsonObject = JSON.create(                  JSON.dic(                          "someKey", "someValue",                          "someArrayKey", JSON.array(                                  "first",                                  1,                                  2,                                  JSON.dic(                                          "emptyArrayKey", JSON.array()                                  )                          )                  )          );        String jsonString = generatedJsonObject.toString();</pre>    <p>result:</p>    <pre>  {    "someKey": "someValue",    "someArrayKey": [      "first",      1,      2,      {        "emptyArrayKey": []      }    ]  }</pre>    <p>add, edit, remove:</p>    <p>note: now its work for first level only</p>    <pre>  generatedJsonObject.addEditWithKey("someArrayKey","someOtherValue");</pre>    <p>result:</p>    <pre>  {    "someKey": "someValue",    "someArrayKey": "someOtherValue"  }</pre>    <h2>Available methods - generate, edit, remove</h2>    <table>     <thead>      <tr>       <th>Method</th>       <th>Input type</th>       <th>Description</th>      </tr>     </thead>     <tbody>      <tr>       <td>add()</td>       <td>Object</td>       <td>if current related object is an array add object at end of array</td>      </tr>      <tr>       <td>remove()</td>       <td>Object</td>       <td>if current related object is an array and input object exist, remove it from array</td>      </tr>      <tr>       <td>addWithIndex()</td>       <td>Object, int</td>       <td>if current related object is an array replace input object with object for input index</td>      </tr>      <tr>       <td>addWithIndex()</td>       <td>Object, int</td>       <td>if current related object is an array replace input object with object for input index</td>      </tr>      <tr>       <td>removeWithIndex()</td>       <td>int</td>       <td>if current related object is an array remove object for input index</td>      </tr>      <tr>       <td>addEditWithKey()</td>       <td>String, Object</td>       <td>if current related object is a dictionary add input object with input key</td>      </tr>      <tr>       <td>removeWithKey()</td>       <td>String</td>       <td>if current related object is a dictionary remove object with input key</td>      </tr>      <tr>       <td>static create()</td>       <td>Object</td>       <td>create new instance of JSON with input object</td>      </tr>      <tr>       <td>static dic()</td>       <td>Object...</td>       <td>if number of input objects is even create JSONObject use even objects as key and odd obejcts as value</td>      </tr>      <tr>       <td>static array()</td>       <td>Object...</td>       <td>create JSONArray use input objects</td>      </tr>     </tbody>    </table>    <p> </p>    <p> </p>    <p> </p>