Android的JSON解析器得到相同的值,每次 [英] Android JSON parser getting the same value everytime

查看:149
本文介绍了Android的JSON解析器得到相同的值,每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,M试图解析这是我从网络服务器接收作为响应JSON字符串,但我有一个奇怪的问题。在我的JSON我有几个一个JSONObjects ,我永远不知道会有什么他们的名字,我应该循环他们根据多少有。但问题是,我使用它甚至得到相同的值,每次当我知道有其他值太code。

I;m trying to parse a json string which I receive from web server as a response, but I have a strange issue. In my json I have a few JSONObjects, which I never know what will be their names and I should cycle them depending on how much keys it has. But the problem is with the code I'm using it's getting the same value everytime even when I know that there is other values too.

下面是我使用的鳕鱼:

JSONObject json = (JSONObject) new JSONTokener(jsonBuffer).nextValue();


        JSONObject country = json.getJSONObject(String.valueOf(json.keys().next()));
        Iterator<Object> keys = json.keys();
        while (keys.hasNext()) {


            String countryName = country.getString("country_name");
            Log.e("","country_name: "+countryName);

            String data = country.getString("data");
            Log.e("","data : "+data);
        }

和这里是我的JSON是什么样子:

and here is what my json looks like :

"AZ": {
    "country_name": "Azerbaijan",
    "data": {
        "181261": {
            "time_published": "2012-04-04 15:55:29",
            "title": "Azerbaijan, Turkey not to change stakes in TANAP",
            "body": null
        },
        "181260": {
            "time_published": "2012-04-04 15:53:10",
            "title": "SOCAR mulls acquisition of Swiss refinery",
        },
        "181061": {
            "time_published": "2012-04-03 05:53:00",
            "title": "Azerbaijan, Lithuania mull LNG terminal investment",
        },
        // and so on....
    }
}, // and it keeps going on 

任何想法我jsonParser应该怎么样子?

Any ideas how my jsonParser should looks like?

推荐答案

我不是很熟悉与Android和Java,但我认为它应该是:

I'm not very familiar with Android and Java, but I assume it should be:

// parse JSON
JSONObject json = (JSONObject) new JSONTokener(jsonBuffer).nextValue();

// get all keys (they are probably strings)
Iterator<String> keys = json.keys();

// as long as there are more keys
while (keys.hasNext()) {
    // get the object corresponding to the next key
    JSONObject country = json.getJSONObject(keys.next());

    String countryName = country.getString("country_name");
    Log.e("","country_name: "+countryName);

    String data = country.getString("data");
    Log.e("","data : "+data);
}

这是我的意见:

在我看来,你有无尽的,而循环,但实际上从来没有失控分配一个新值国家

It looks to me that you have an endless while loop, it actually never gets out to assign a new value to country.

您必须遍历所有的键,得到关键国家对象,然后访问它的值。

You have to iterate over all keys, get the country object for the key, and then access it's values.

目前你所得到的第一个国家的元素,拿到钥匙,只是检查迭代器是否包含关键字。但你不是推进迭代器。

Currently you are getting the first country element, get the keys and just check whether the iterator contains keys. But you are not advancing the iterator.

这篇关于Android的JSON解析器得到相同的值,每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆