json字符串转换为JSONObject和JSONArray
来源:互联网 发布:iope水乳 知乎 编辑:程序博客网 时间:2024/05/20 18:02
具体转化过程
//JSONObject
String jsonMessage = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";
String value1 = null;
try
{
//将字符串转换成jsonObject对象
JSONObject myJsonObject = new JSONObject(jsonMessage);
//获取对应的值
value1 = myJsonObject.getString("数学");
}
catch (JSONException e)
{
}
System.out.println("value1="+value1);
//JSONArray
jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +
"{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +
"{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";
JSONArray myJsonArray;
try
{
myJsonArray = new JSONArray(jsonMessage);
for(int i=0 ; i < myJsonArray.length() ;i++)
{
//获取每一个JsonObject对象
JSONObject myjObject = myJsonArray.getJSONObject(i);
//获取每一个对象中的值
String numString = myjObject.getString("num");
int englishScore = myjObject.getInt("外语");
int historyScore = myjObject.getInt("历史");
int geographyScore = myjObject.getInt("地理");
//获取数组中对象的对象
JSONObject myjObject2 = myjObject.getJSONObject("object");
String aaaString = myjObject2.getString("aaa");
System.out.println("aaaString="+aaaString);
System.out.println("numString="+numString);
System.out.println("englishScore="+englishScore);
System.out.println("historyScore="+historyScore);
System.out.println("geographyScore="+geographyScore);
}
}
catch (JSONException e)
{
}
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray .
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- JSONArray,JSONObject和字符串的相互转换
- JSONObject,JSON,和JSONArray
- fastjson对json字符串JSONObject和JSONArray互相转换操作示例
- Json的JSONArray和JSONObject
- java json字符串转JSONObject和JSONArray以及取值
- java json字符串转JSONObject和JSONArray 循环取值
- myeclipse中的complier
- Android 高效加载大图片避免OOM
- 一些工具类
- android 数据图标的刷新通知流程
- Java Web学习总结(13)——JSP入门
- json字符串转换为JSONObject和JSONArray
- 电子商务是未来的方向
- [VC] 【Visual Studio】2005~2015中文完整旗舰版(附序列号)
- 面试放鸽子,真的不是我的错!
- SD/MMC CSD寄存器 V1.0和V2.0详解(如何读写SD/MMC卡)
- maven学习(下)利用Profile构建不同环境的部署包
- JAVA注解
- 微信,qq浏览器不支持链接下划线的问题
- Linux命令之expect