[英]Json parsing in an Android application
I am new in Json parsing. 我是Json解析的新手。 I am receiving Json data from my url which is given below: 我正在从我的URL接收Json数据,如下所示:
[
[
{
"message": "hdjcjcjjckckckckvkckckck",
"timetoken": 14151866297757284
},
{
"message": "ufjfjfjcjfjchfjdhwjroritkgjcj",
"timetoken": 14151869212145692
},
{
"message": "udjfjfudjcyshdhsnfkfkvkf",
"timetoken": 14151869317015766
},
{
"message": "lvkifjsywjfwhsjvjdjfudjgidufkg",
"timetoken": 14151869404695072
},
{
"message": "ifjfydncydhsxhshfjdjejtlfudj",
"timetoken": 14151869494732788
},
{
"message": "22637485969473849506&#&#*%-&+",
"timetoken": 14151869589393336
},
{
"message": "jcjfjdueywjfusufig",
"timetoken": 14151869671994892
},
{
"message": "ofkdiriflfkkkfkdiidk",
"timetoken": 14151869775170644
},
{
"message": "testing",
"timetoken": 14151869895179728
},
{
"message": 1234567,
"timetoken": 14151869986900556
},
{
"message": 9877653,
"timetoken": 14151870106439620
},
{
"message": "zxcvbnmmkljgdaqerip",
"timetoken": 14151870236042386
}
],
14151866297757284,
14151870236042386
]
I am using this to break JsonArray data into different index like I want to display message and timetoken in separate lines in my activity, like this: 我正在使用它来将JsonArray数据分为不同的索引,就像我想在活动中的不同行中显示消息和timetoken一样,如下所示:
message: hdjcjcjjckckckckvkckckck
time token: 14151866297757284
message: 22637485969473849506&#&#*%-&+
time token: 14151869212145693
What should I have to do in the following line of codes: 我在下面的代码行中应该做什么:
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
for (int i = 0; i < jsonObj.length(); i++) {
}
I want to display it in my Textview
as described above. 我想如上所述在我的Textview
显示它。
Try this one (not tested): 试试这个(未测试):
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
JSONArray array = jsonObj.getJSONArray(0);
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String mesage = item.getJsonString("message");
String timespan = item.getJsonString("timespan");
}
I have solved the problem. 我已经解决了问题。 Actually the Json string has double Json Array and then Json Object. 实际上,Json字符串具有双重Json Array,然后具有Json Object。
I was doing a mistake to send an object of Json Array to Json Object. 我在将Json Array的对象发送给Json Object时犯了一个错误。 Now I have make an object of Json Array1 then send it to Json Array2 and then send the object of Json Array2 in Json Object
. 现在,我制作了一个Json Array1对象,然后将其发送到Json Array2,然后在Json Object
发送了Json Array2 Json Object
。
The code is given below: 代码如下:
try {
JSONArray jsonObj = new JSONArray(message.toString());
JSONArray jArray = new JSONArray(jsonObj.get(0).toString());
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
String messageString=c.getString("message");
String timeString=c.getString("timetoken");
String abc = timeString;
}
}
you should put json parsing into try-catch block: 您应该将json解析放入try-catch块中:
try{
JSONArray res = new JSONArray(response.toString());
JSONArray jsonArray = res.getJSONArray(0);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String message = object.getString("message");
String token = object.getString("timetoken");
}
}catch(Exception e){
e.printStackTrace();
}
you have a double array, so you have two JSONArray. 您有一个双精度数组,因此您有两个JSONArray。 Actually, JSONArray usually marked by [] and JSONObject marked by {}. 实际上,JSONArray通常用[]标记,而JSONObject通常用{}标记。
In other words {}
= JSONObject
and []
= JSONArray
. 换句话说, {}
= JSONObject
和[]
= JSONArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.