5

I have been trying to follow along with this solution How to find specified name and its value in JSON-string from Java?

However it does not seem to make sense.

I define a new gson object from a string:

Example of string here: http://api.soundrop.fm/spaces/XJTt3mXTOZpvgmOc

public void convertToJson()
{
    Gson gson = new Gson();
    Object gsonContent = gson.fromJson( stringContent, RadioContent.class );
}

And then try and return a value:

public Object getValue( String find )
{
    return gsonContent.find;
}

Finally its called with:

public static void print( String find = "title" )
{
    Object value = radioContent.getValue( find );

    System.out.println( value );
}

However I am getting an error:

java: cannot find symbol
  symbol:   variable find
  location: variable gsonContent of type java.lang.Object

Full classes: Main class: http://pastebin.com/v4LrZm6k Radio class: http://pastebin.com/2BWwb6eD

Community
  • 1
  • 1
Daniel
  • 357
  • 1
  • 2
  • 12
  • First got to json.org and study the JSON syntax. There are two types of JSON structures, "objects" and "arrays". Everything is built by layering those together. "Objects" you access using keys, while "arrays" are accessed with a numeric index. Depending on the toolkit you use the "deserialized" JSON will be represented as plain old Java Maps ("objects") and Lists ("arrays") or special Map/List-like classes belonging to the JSON kit you use. (It's important to note that the JSON "object" is not the same as the Java "Object".) – Hot Licks Feb 07 '14 at 19:39
  • Essentially, all I want to do is get the value of a key. So I'm guessing objects are what I should be working with. – Daniel Feb 07 '14 at 19:43
  • Parse the JSON as a `JsonObject` and retrieve the field you need. – Sotirios Delimanolis Feb 07 '14 at 19:45
  • Retrieving the field is what I have been trying to achieve here. – Daniel Feb 07 '14 at 19:48
  • 1
    In the general case (without looking at the incoming JSON), when you parse JSON you don't know if you get back an object or an array. So you'd use `instanceof` to check which, and then cast to the appropriate type. If you "trust" the JSON to be a certain way you can skip that and parse directly as an object. In either case, once you have an object (Java Map), you can `get` the element you want using the appropriate key. Of course, if you want something several layers in you must "peel the onion" to get there. – Hot Licks Feb 07 '14 at 20:51
  • (Unfortunately, Gson hides this level of access, forcing you to use a more obscure approach.) – Hot Licks Feb 07 '14 at 20:57
  • I read about that, I need to do something along the lines of: people = jsonobject.get(people), then person = people.get(person) – Daniel Feb 07 '14 at 21:15

1 Answers1

20

This is Java. Fields are resolved based on the declared type of the object reference.

Based on your compiler error, gsonContent is a variable of type Object. Object does not have a find field.

You're already telling Gson what type to deserialize to, so just make the gsonContent variable be of that type

RadioContent gsonContent = gson.fromJson( stringContent, RadioContent.class );

Also, it seems like you are shadowing the instance gsonContent field with a local variable.


You can do the following as well

JsonObject jsonObject = gson.fromJson( stringContent, JsonObject.class);
jsonObject.get(fieldName); // returns a JsonElement for that name
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724