繁体   English   中英

Json在Android应用程序中解析

[英]Json parsing in an Android application

我是Json解析的新手。 我正在从我的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
]

我正在使用它来将JsonArray数据分为不同的索引,就像我想在活动中的不同行中显示消息和timetoken一样,如下所示:

message: hdjcjcjjckckckckvkckckck
time token: 14151866297757284
message: 22637485969473849506&#&#*%-&+
time token: 14151869212145693

我在下面的代码行中应该做什么:

JSONArray jsonObj = new JSONArray(message.toString());  //message.toString() is the Json Response data
for (int i = 0; i < jsonObj.length(); i++) {

}

我想如上所述在我的Textview显示它。

试试这个(未测试):

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");
}

我已经解决了问题。 实际上,Json字符串具有双重Json Array,然后具有Json Object。

我在将Json Array的对象发送给Json Object时犯了一个错误。 现在,我制作了一个Json Array1对象,然后将其发送到Json Array2,然后在Json Object发送了Json Array2 Json Object

代码如下:

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;

            }

        }

您应该将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();
}

您有一个双精度数组,因此您有两个JSONArray。 实际上,JSONArray通常用[]标记,而JSONObject通常用{}标记。

换句话说, {} = JSONObject[] = JSONArray

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM