简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储

来源:互联网 发布:上海高级美工培训 编辑:程序博客网 时间:2024/05/21 08:55

本文标签: Android开发技巧json解析Android数据存储

JSON


简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储.

添加到项目

To use JSON you must add it as a dependency in your Gradle build:

Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:

allprojects {        repositories {            ...            maven { url "https://jitpack.io" }        }    }

Step 2. Add the dependency

dependencies {    compile 'com.github.amirdew:JSON:v1.0.0'}

使用

You can create JSON object from string and access data with key() and index() methods.

String simpleJsonString = "{\"id\":1,\"name\":\"A green door\",\"price\":12.5,\"tags\":[\"home\",\"green\"]}";JSON json = new JSON(simpleJsonString);//access dataString firstTag = json.key("tags").index(0).stringValue();Double price = json.key("price").doubleValue();

products json:

[  {    "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    }  }]

loop:

for(int i=0; i<products.count(); i++){   json productInfo = products.index(i);   String productName = productInfo.key("name").stringValue();}

JSON is exception and null free, you can use key() and index() many times without worry about any exception.

int someValue = products.index(8).key("someKey").index(1).key("someOther").intValue(); //someValue = 0

check index or key is exist or is null:

if( products.index(3).key("someKey").isNull() ){     /*...*/  }if( products.index(1).key("someKey").exist() ){   /*...*/  }

方法

MethodInput typeReturn typeDefaultDescriptionkey()StringJSON-if object is a dictionary return JSON object with input keyindex()intJSON-if object is a array return JSON object with input indexstringValue()-Stringempty string ("")return .toString() of objectintValue()-int0return integer value if possiblelongValue()-long0return long value if possibledoubleValue()-Double0return Double value if possiblebooleanValue()-booleanfalsereturn true if object is kind of boolean and true or is kind of string and equal "true"value()-Object-return related objectcount()-int0if related object is a dictionary return number of keys, if related object is a array return length of arrayisNull()-boolean-return true if related object is nullexist()-boolean-return true if related object with index or key existsgetJsonArray()-JSONArraynullreturn related JSONArraygetJsonObject()-JSONObjectnullreturn related JSONObject

用法 - 生成json,数据保存

You can use JSON.dic() and JSON.array() static methods to generate json string or hold and pass data

JSON generatedJsonObject = JSON.create(                JSON.dic(                        "someKey", "someValue",                        "someArrayKey", JSON.array(                                "first",                                1,                                2,                                JSON.dic(                                        "emptyArrayKey", JSON.array()                                )                        )                )        );  String jsonString = generatedJsonObject.toString();

result:

{  "someKey": "someValue",  "someArrayKey": [    "first",    1,    2,    {      "emptyArrayKey": []    }  ]}

add, edit, remove:

note: now its work for first level only

generatedJsonObject.addEditWithKey("someArrayKey","someOtherValue");

result:

{  "someKey": "someValue",  "someArrayKey": "someOtherValue"}

Available methods - generate, edit, remove

MethodInput typeDescriptionadd()Objectif current related object is an array add object at end of arrayremove()Objectif current related object is an array and input object exist, remove it from arrayaddWithIndex()Object, intif current related object is an array replace input object with object for input indexaddWithIndex()Object, intif current related object is an array replace input object with object for input indexremoveWithIndex()intif current related object is an array remove object for input indexaddEditWithKey()String, Objectif current related object is a dictionary add input object with input keyremoveWithKey()Stringif current related object is a dictionary remove object with input keystatic create()Objectcreate new instance of JSON with input objectstatic dic()Object...if number of input objects is even create JSONObject use even objects as key and odd obejcts as valuestatic array()Object...create JSONArray use input objects

amirdew /JSON

Star10Fork1

项目描述:simple and easy json parser, json generator, and data holder based on JSONArray and JSONObject for android —查看更多内容...

本文标签: Android开发技巧json解析Android数据存储

转自 SUN'S BLOG - 专注互联网知识,分享互联网精神!

原文地址: 《简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储》


原文地址:http://whosmall.com/?post=290


0 0
原创粉丝点击