11

I am wondering if there is a way to get the value of the first child of a JSONObject without knowing its name:

I have some JSON coming in with a node called, this_guy

{"this_guy": {"some_name_i_wont_know":"the value i care about"}}

Using JSONObject, how can I get "the value i care about," cleanly if I don't know the name of the child. All I know is "this_guy", anyone?

LearningProcess
  • 597
  • 1
  • 4
  • 27
erik
  • 4,698
  • 11
  • 64
  • 111
  • is the position of the value "the value i care about" fixed in each JSON object ? – AbtPst Nov 04 '15 at 20:30
  • @AbtPst can't. depending on the implementation, the position of values in json objects does not necessarily depends on the position in the json strings. (and the position altogether may be not available, e.g. if the json parser back objects with hashmaps) – njzk2 Nov 04 '15 at 20:51
  • hmm, well there must be something you know about the position of the value you seek? if you dont know the key and you dont know the poisition/index then there is no way to figure out where the value is. – AbtPst Nov 04 '15 at 20:53
  • what i can rely on is that the services people will only be returning one child for this object.. i argued for just {"this_guy":"the value i care about"} but they insist on the above format, – erik Nov 04 '15 at 20:54
  • oh so just get the "this_guy" object and extract the value. look at my answer – AbtPst Nov 04 '15 at 20:59

3 Answers3

20

Use JSONObject.keys() which returns an iterator of the String names in this object. then use these keys to retrieve values.

To get only first value:

 Iterator<String> keys = jsonObject.keys();
 // get some_name_i_wont_know in str_Name
 String str_Name=keys.next(); 
 // get the value i care about
 String value = json.optString(str_Name);
ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
0
Object obj = parser.parse(new FileReader("path2JsonFIle"));

JSONObject jsonObject = (JSONObject) obj;

try this iterator

JSONObject jsonObj = (JSONObject)jsonObject.get("this_guy");

for (Object key : jsonObj.keySet()) {
        //based on you key types
        String keyStr = (String)key;
        Object keyvalue = jsonObj.get(keyStr);

        /*check here for the appropriate value and do whatever you want*/
        //Print key and value
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

    }

once you get the appropriate value, just break out of the loop. for example you said that all you need is the first value in the inner map. so try womething like

int count = 0;

String valuINeed="";
for (Object key : jsonObj.keySet()) {
            //based on you key types
            String keyStr = (String)key;
            Object keyvalue = jsonObj.get(keyStr);

            valueINeed = (String)keyvalue;
            count++;

            /*check here for the appropriate value and do whatever you want*/
            //Print key and value
            System.out.println("key: "+ keyStr + " value: " + keyvalue);

            if(count==1)
                break;

        }

          System.out.println(valueINeed);
AbtPst
  • 6,676
  • 13
  • 69
  • 138
0

if you care only about the value and not about the key, you can use directly this:

Object myValue = fromJson.toMap().values().iterator().next();
xMichal
  • 554
  • 1
  • 6
  • 19