how to access JSON DATA use key value such as "name" {"name":"young"}

0 votes

 

{"name":"zet", "HP":1000, "MP":2000 } --> THIS IS MY JSON COLLECTION

I want to get data value use key like "name " and get value "zet" or use key "HP" and get 1000 

how can i access  jsonDocList.get(i).getJsonDoc()  use key value?

i found function like  -> String strName = jsonDocList.get(i).getJsonDoc().getkey("zet");

 

 

 

ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList();              

        for(int i=0;i<jsonDocList.size();i++)  

        {  

            strSWORDObjUID = jsonDocList.get(i).getDocId();

             System.out.println("ParseMyHero jsonSwordHero objectId is " + jsonDocList.get(i).getDocId());    

            System.out.println("ParseMyHero jsonSwordHero CreatedAt is " + jsonDocList.get(i).getCreatedAt());    

            System.out.println("ParseMyHero jsonSwordHero UpdatedAtis " + jsonDocList.get(i).getUpdatedAt());    

            System.out.println("ParseMyHero jsonSwordHero Jsondoc is " + jsonDocList.get(i).getJsonDoc());  

 

 

        }   

 

asked Feb 3, 2016 in Android by zetcrusader (13 points)
recategorized Feb 4, 2016 by shepAdmin

1 Answer

+1 vote

Hi Young,

Iterating key values from JSON document is very simple, you can use iterator as shown below:

JSONObject jsonDocument = new JSONObject(jsonDocList.get(i).getJsonDoc());
Iterator<?> keys = jsonDocument.keys();
while( keys.hasNext() ) {
   String key = (String)keys.next();
   Object value = jsonDocument.get(key);
   System.out.println("Document key: " + key + "Value is: "+value);
}
 
Using this line(Object value = jsonDocument.get(key);) of code inside for loop, you can get the values of your desired keys.  For example: name, HP etc
 
Let me know if it helps.
 
regards,
Himanshu Sharma
answered Feb 3, 2016 by hs00105 (2,005 points)
thank you for your help!
Can you provide an example of iterating key values using Corona?
Hi Brian,

You can use sortByKeyValue method as shown below:

for key, value in sortKeyByValue(table) do
       print(key..value)
    end

Regards,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...