具体到上找Java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了
二.具体转化过程
获取对应的值
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'," +
"{'num':'爱好','外语':48,'历史':62,'地理':39,'object':{'aaa':'11c11','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的方法,希望能够帮助到大家,如果大家有疑问欢迎给我留言,小编会及时回复大家,在此也非常感谢大家对编程之家网站的支持!