Retriving value from Json (codenameone)

167 views
Skip to first unread message

Aayushma Nepal

unread,
Sep 7, 2015, 4:27:26 AM9/7/15
to CodenameOne Discussions
how can i retrive data from Json..Here is my code
 protected void onMain_ButtonAction(final Component c, ActionEvent event) {

        ConnectionRequest r = new ConnectionRequest() {
            Hashtable h;
            

            @Override
            protected void readResponse(InputStream input) throws IOException {

                JSONParser p = new JSONParser();
                h = p.parse(new InputStreamReader(input));
               ArrayList<Map<String, Object>> a = new ArrayList<Map<String, Object>>();
                HashMap<String, Object> m = new HashMap<String, Object>();
                m.put("Label1",h.toString());
                 for (String key : m.keySet()) {
                
                     a.add(m);
                    System.out.println("key: " + key );
 };
        //  NetworkManager.getInstance().addToQueue(request); 
        r.setPost(false);
        InfiniteProgress prog = new InfiniteProgress();
        Dialog dlg = prog.showInifiniteBlocking();
        r.setDisposeOnCompletion(dlg);
        NetworkManager.getInstance().addToQueue(r);
}
Please help me for this...Thanks


If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

BK Shrestha

unread,
Sep 7, 2015, 5:29:53 AM9/7/15
to CodenameOne Discussions
Hi,

Your response is in json array. So you need to get "root" value first. Check this code. 
JSONParser j = new JSONParser();
                        InputStreamReader isr = new InputStreamReader(input);
                        Map<String, Object> h = j.parseJSON(isr);
                        Log.p(h.toString()); // this will give value including root field
                        ArrayList<Map<String, String>> map = (ArrayList<Map<String, String>>) h
                                .get("root");
                        
                        Log.p(map.toString());

Now you can do whatever you like with the Arraylist map.

Thanks!

Aayushma Nepal

unread,
Sep 7, 2015, 6:02:36 AM9/7/15
to CodenameOne Discussions
how to put the retrive data from json in list...

 m.put("Label1",h.toString());// Label1 is the label name from render
                   for (Object s : o.values()) {
                System.out.println(s);
                a.add(s);//here is my problem, how to display individual value of hashmap 

Shai Almog

unread,
Sep 8, 2015, 12:40:50 AM9/8/15
to CodenameOne Discussions
This:

for (String key : m.keySet()) {                

    a
.add(m);
   
System.out.println("key: " + key );
 
}


Is wrong if you just want the values... You are adding the entire hashmap to the ArrayList.

Use:
for (String key : m.keySet()) {                
    a
.add(m.getValue());
}


Reply all
Reply to author
Forward
0 new messages