0

I have the following example json output form java/jsonsimple.

The JSONArray is:

[{"dd1":{"actionType":"Dept","collegeID":""}},{"dd2":{"actionType":"Dept","collegeID":""}}] 

I'm trying to figure out how to get the value for the "dd1" key by using the "dd1" instead of the ndx.

basically, I want to be able to test for a given "key" to see if it's in the array, with a value.

The docs for JSONArray, allow for foo.get(1), but not foo.get("dd1")

thanks

ps.

I tried to use the insert the above into a JSONObject, and then do a foo.get("dd1") but I didn't get the value.

The issue appears to come down to figuring out how to get dict that's in the [] array.

doing something like foo.get(1).get("dd1") doesn't work...

thanks

tom smith
  • 1,035
  • 7
  • 23
  • 39

2 Answers2

2

You have an array wrapping your dictionary in your JSON input, so you'd have to get the dictionary first, something like:

foo.get(1).get("dd1");

But you can change your JSON to be a simple dictionary, like this:

{
   "dd1": {"actionType":"Dept","collegeID":""},
   "dd2": {"actionType":"Dept","collegeID":""}
}

And then you'll be able to do:

foo.get("dd1");
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
  • hi. foo is a JSONArray. if I do the foo.get(1), i get the 2nd element of the array, which has the "dd2".. so I need a way to somehow get the dictionary from the array. – tom smith Dec 05 '11 at 13:21
  • oh, then you need to do `foo.get(0);` ;-) `foo.get(0)` will get the first dictionary, and `foo.get(0).get("dd1")` will get the inner dictionary. – Elias Dorneles Dec 05 '11 at 13:44
  • hi eljunior.... neither foo.get(0), or foo.get(1) allow me to get the initial dict. if i do a foo.get(0) i have the 1st array/dict element, which has "dd1". i could then do a get("dd1") on the returned item.. but that's useless, as i would already have the "dd1" item. what i'm trying to accomplish is to be able to do a direct .get("dd1") so i can determine if the key exists, and to get the associated value... so if you know of a way to convert what i have, to then be able to just do a abc.get("dd1").. i'm game! thanks – tom smith Dec 05 '11 at 14:02
  • 1
    It goes like this: for an input `[{"one":1},{"two":2}]`, if you do `foo.get(0)`, you have the first dictionary: `{"one":1}`. If you do: `foo.get(0).get("one")` you'll have `1`. So, if you want to do foo.get("dd1") and get the dictionary, your input would have to be: `{"dd1":{"dd1":{"actionType":"Dept","collegeID":""}}}`, but then you would've replicated information with no substantial gain (unless your real dictionary is more complex than what you posted). Only dictionaries allow to access elements by key. Hope it helps! – Elias Dorneles Dec 05 '11 at 15:18
  • this didn't work for me. for example I have; `JSONArray arr = [{"01":"key1"},{"02":"key2"},{"03":"key3"}]` `arr.get("01")` throws an error. – mmu36478 Mar 01 '17 at 12:04
0

You are mixing up json array and json object. With foo.get(1) you will retrieve JSON object, containing field "dd1" - then you can use get("dd1")

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35