Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving all the keys in a nested json in java
    primarykey
    data
    text
    <p>This is the program i wrote:</p> <pre><code> /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication1; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; import net.sf.json.JSONException; import net.sf.json.JSONObject; /** * * @author 311001 */ public class NewClass { public static void main(String args[]) { JSONObject parentData = new JSONObject(); JSONObject childData = new JSONObject(); try { parentData.put("command", "login"); parentData.put("uid", "123123123"); childData.put("uid", "007"); childData.put("username", "sup"); childData.put("password", "bros"); parentData.put("params", childData); System.out.println(parentData); Map&lt;String, String&gt; map = new HashMap&lt;&gt;(); Iterator&lt;?&gt; iter = parentData.keys(); while (iter.hasNext()) { String key = (String) iter.next(); String value = parentData.getString(key); map.put(key, value); } for (Entry&lt;String, String&gt; entry : map.entrySet()) { System.out.println("key &gt; " + entry.getKey() + " : value = " + entry.getValue()); } String testData = map.get("params.uid"); System.out.println(testData); System.out.println("Tokenizing json"); String resultStr = parentData.toString(); System.out.println("String tokens "); StringTokenizer st = new StringTokenizer(resultStr); System.out.println(st.countTokens()); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } String testDat="abc :: result"; StringTokenizer simpleString = new StringTokenizer(testDat); System.out.println("Tokenizing simple string"); System.out.println(simpleString.countTokens()); while (simpleString.hasMoreTokens()) { System.out.println(simpleString.nextToken()); } } catch (JSONException e) { e.printStackTrace(); } } } </code></pre> <p>the output i got:</p> <pre><code>run: {"command":"login","uid":"123123123","params":{"uid":"007","username":"sup","password":"bros"}} key &gt; uid : value = 123123123 key &gt; command : value = login key &gt; params : value = {"uid":"007","username":"sup","password":"bros"} null Tokenizing json String tokens 1 {"command":"login","uid":"123123123","params":{"uid":"007","username":"sup","password":"bros"}} Tokenizing simple string 3 abc :: result BUILD SUCCESSFUL (total time: 0 seconds) </code></pre> <p>How can I recieve all the keys in my json object. In case I tokenize why do i get only one string token while for a simple string am getting the correct output 3 tokens.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload