package lantrack.com.jsondemo_orgjson;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String jsonObjectString = "{\"userId\":981,\"GroupID\":2,\"userNickName\":\"xxx\"," +
"\"userRealName\":\"风清扬1223\",\"xx\":{\"yy\":\"zz\"}}";
private static final String jsonArrayString = "[{\"userId\":981,\"GroupID\":2,\"userNickName\":\"xxx\"," +
"\"userRealName\":\"风清扬1223\"},{\"userId\":123,\"GroupID\":1,\"userNickName\":\"yyy\"," +
"\"userRealName\":\"abc\"}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parseJsonObject();
parseJsonArray();
}
private void parseJsonObject() {
try {
// 新建JSONObject
JSONObject jsonObject = new JSONObject(jsonObjectString);
// 直接可得数据
String userId = jsonObject.getString("userId");
// 打印userId
Log.d(TAG, userId);
// 先取得 xx的值,是个JSONObject
JSONObject yyObject = jsonObject.getJSONObject("xx");
// 再 取得yy的值
String yy = yyObject.getString("yy");
Log.d(TAG, yy);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void parseJsonArray() {
try {
// 新建JSONArray
JSONArray jsonArray = new JSONArray(jsonArrayString);
// 得到数组下标为0的JSONObject
JSONObject jsonObject0 = jsonArray.getJSONObject(0);
//
String userId = jsonObject0.getString("userId");
// 打印userId
Log.d(TAG, userId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android Json解析之org.json
共53个文件
xml:20个
bin:6个
png:5个
需积分: 10 7 下载量 105 浏览量
2016-04-28
10:52:00
上传
评论
收藏 188KB RAR 举报
温馨提示
我们通常在Android上采用Gson来解析Json数据,很方便的就可以把数据转换成List或者map。当碰到日期时间的时候,你可能会遇到坑,需要格式化一下日期时间格式。 本文介绍org.json这种不需要导入任何包的解析方式,不管Json多少层,如探囊取物。 详见:http://blog.csdn.net/hwe_xc/article/details/51271815
资源推荐
资源详情
资源评论
收起资源包目录
JSONDemo_orgjson.rar (53个子文件)
JSONDemo_orgjson
gradlew.bat 2KB
gradlew 5KB
JSONDemo_orgjson.iml 947B
gradle.properties 872B
.gradle
2.10
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 19KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 25KB
taskArtifacts
fileHashes.bin 124KB
cache.properties.lock 17B
taskArtifacts.bin 71KB
fileSnapshots.bin 910KB
cache.properties 31B
outputFileStates.bin 20KB
app
proguard-rules.pro 697B
src
test
java
lantrack
com
jsondemo_orgjson
ExampleUnitTest.java 336B
androidTest
java
lantrack
com
jsondemo_orgjson
ApplicationTest.java 372B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 82B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 725B
values-w820dp
dimens.xml 364B
java
lantrack
com
jsondemo_orgjson
MainActivity.java 2KB
AndroidManifest.xml 688B
app.iml 9KB
.gitignore 8B
build.gradle 656B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
.idea
gradle.xml 739B
runConfigurations.xml 564B
misc.xml 2KB
.name 16B
copyright
profiles_settings.xml 76B
modules.xml 369B
encodings.xml 159B
compiler.xml 686B
workspace.xml 138KB
libraries
support_v4_22_2_1.xml 773B
hamcrest_core_1_3.xml 526B
junit_4_12.xml 477B
appcompat_v7_22_2_1.xml 629B
support_annotations_22_2_1.xml 583B
.gitignore 105B
local.properties 464B
settings.gradle 16B
build.gradle 521B
共 53 条
- 1
资源评论
StormWatch
- 粉丝: 12
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功