简体   繁体   English

不知道如何在android中检索JSON数据(尤其是这)

[英]dont know how to retrieve JSON data in android (this in particular)

i been reading and searching all over the net, and i still can find the best(easiest) way to retrieve this JSON info, if u ask me, the structure of it is very messy coz the ID of blocks, is also a data you need to obtain... =/ 我一直在网上阅读和搜索,但我仍然可以找到最好的(最简单的)方法来检索此JSON信息,如果您问我,它的结构非常混乱,因为块的ID也是您的数据需要获得... = /

{"blocks": 
{"305795": {"is_mature": 1, "total_score": 1, "mining_duration": "1:39:26", "date_found": "2014-06-14 18:25:27", "confirmations": 1, "total_shares": 1, "date_started": "2014-06-14 16:46:01", "reward": "1", "nmc_reward": "0E-8"}, 
 "305796": {"is_mature": 1, "total_score": 1, "mining_duration": "6:17:00", "date_found": "2014-06-15 11:29:32", "confirmations": 1, "total_shares": 1, "date_started": "2014-06-15 05:12:32", "reward": "1", "nmc_reward": "0E-8"}
}

the problem that i have, is that i need the Block Number as a field the same way i need the other keys... to make a basic table with all the data by rows... 我遇到的问题是,我需要像其他字段一样使用块号作为字段...用行创建所有数据的基本表...

i have this ATM. 我有这个自动取款机。

JSONObject json_blocks = json.getJSONObject("blocks");

        //Log.e("JSON Debug", "Number of Blocks: " + String.valueOf(json_blocks.length()));    

        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();            
        // looping through All Contacts
        Iterator iter = json_blocks.keys();
        while(iter.hasNext()){
            String key = (String)iter.next();
            String value = (String) json_blocks.getString(key);
            System.out.println("Key: "+key+" Value: "+value);
            map.put(key,value);
        }

but i get all the fields inside blocks as a huge string =( Please, can someone give me some tips?... thanks! 但是我将块中的所有字段都当作一个巨大的字符串=(请,有人可以给我一些提示吗?...谢谢!

Instead of doing json_blocks.getString(key) you should use json_blocks.opt(key) which returns an object. 代替执行json_blocks.getString(key)您应该使用json_blocks.opt(key)返回一个对象。 Here you can check if the returned value is an instance of JSONObject . 在这里,您可以检查返回的值是否是JSONObject的实例。 If it is, cast it to it, and do the additional get calls you need to get your data. 如果是,则将其强制转换为它,然后执行获取数据所需的其他get调用。

Alternativley, you can attempt json_blocks.optJSONObject(key) . Alternativley,您可以尝试json_blocks.optJSONObject(key) If a value is returned (non null) then cast to JSONObject and process it, otherwise do your getString call if that's what you want. 如果返回一个值(非null),则将其JSONObjectJSONObject并对其进行处理,否则,如果需要,请执行getString调用。

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

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