在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:amirdew/JSON开源软件地址:https://github.com/amirdew/JSON开源编程语言:Java 100.0%开源软件介绍:JSON
Add to your projectTo 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'
} Usage - parse jsonYou 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 data
String 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() ){
/*...*/
} Available methods - parse
Usage - generate json, data holdingYou 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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论