简单易用的Android JSON Parser

simple and easy json parser, json generator, and data holder based on JSONArray and JSONObject for android

Add to your project

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:

Step 2. Add the dependency

Usage – parse json

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

products json:

loop:

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

check index or key is exist or is null:

Available methods – parse

Method Input type Return type Default Description
key() String JSON if object is a dictionary return JSON object with input key
index() int JSON if object is a array return JSON object with input index
stringValue() String empty string (“”) return .toString() of object
intValue() int 0 return integer value if possible
longValue() long 0 return long value if possible
doubleValue() Double 0 return Double value if possible
booleanValue() boolean false return true if object is kind of boolean and true or is kind of string and equal “true”
value() Object return related object
count() int 0 if related object is a dictionary return number of keys, if related object is a array return length of array
isNull() boolean return true if related object is null
exist() boolean return true if related object with index or key exists
getJsonArray() JSONArray null return related JSONArray
getJsonObject() JSONObject null return related JSONObject

Usage – generate json, data holding

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

add, edit, remove:

note: now its work for first level only

result:

Available methods – generate, edit, remove

Method Input type Description
add() Object if current related object is an array add object at end of array
remove() Object if current related object is an array and input object exist, remove it from array
addWithIndex() Object, int if current related object is an array replace input object with object for input index
removeWithIndex() int if current related object is an array remove object for input index
addEditWithKey() String, Object if current related object is a dictionary add input object with input key
removeWithKey() String if current related object is a dictionary remove object with input key
static create() Object create new instance of JSON with input object
static dic() Object… if number of input objects is even create JSONObject use even objects as key and odd obejcts as value
static array() Object… create JSONArray use input objects

Github项目地址:https://github.com/amirdew/JSON

简单易用的Android JSON Parser
Tagged on:             

发表评论

电子邮件地址不会被公开。 必填项已用*标注