简单易用的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() ){ /*...*/ }
方法
用法 - 生成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
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
- 简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储
- json数据解析<一> JSONObject和JSONArray的简单使用
- 用JSONObject和JSONArray 解析json数据
- json 的解析-使用Android中的JSONObject和JSONArray解析json数据
- android用JSONObject和JSONArray解析json格式数据
- jsonObject和jsonArray解析json的方式
- jsonObject和jsonArray解析json的方式
- Android中JSON的解析-JSONObject和JSONArray
- Json的JSONArray和JSONObject
- JSON数据解析(JSONArray和JSONObject)
- Android中的JSONObject和JSONArray解析json数据
- Android JSON 解析(一) JSONObject 和 JSONArray
- Android JSON解析(JSONObject和JSONArray)
- Android JSON解析(JSONObject和JSONArray)
- JSONObject,JSON,和JSONArray
- 使用JSONObject和JSONArray 进行Json解析
- android 读取JSON数据(遍历JSONObject和JSONArray)
- android 读取JSON数据(遍历JSONObject和JSONArray) .
- 从百度统计看到的一些有意思的事情
- linux socket实现内核态和用户态通信
- deeplearning 打卡第四天_线性代数基础_1
- Android与H5的交互
- HRBUST1315-火影忍者之~大战之后
- 简单易用的json解析,json生成器和基于JSONArray和JSONObject for Android的数据存储
- 2017.3.5
- 关于GetWindowTextA()和GetWindowTextW()和GetWindowText()的区别
- Dijkstra算法详解
- hdu5078 Osu!
- 十道海量数据处理面试题与十个方法大总结
- CreateWindow中dwStyle设置为WS_POPUP时显示不出来
- jquery.cookie.js的使用方法
- 用大白话聊聊分布式系统