0

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... =/

{"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!

1

1 Answer 1

1

Instead of doing json_blocks.getString(key) you should use json_blocks.opt(key) which returns an object. Here you can check if the returned value is an instance of JSONObject. If it is, cast it to it, and do the additional get calls you need to get your data.

Alternativley, you can attempt 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that thet getString(key) method has a convenient feature that calls the objects toString() method so that way you get a String if there is an object present. That is why you are getting the long string returned.
thanks so much!!! that worked like a charm =) Iterator iter = json_blocks.keys(); while(iter.hasNext()){ String key = (String)iter.next(); JSONObject value = (JSONObject) json_blocks.opt(key); //String value = (String) json_blocks.getString(key); System.out.println("Key: "+key+" Value: "+value.get("reward")); map.put(key, value.get("reward").toString()); }
I'm glad it worked out. Feel free to accept my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.