Asked  6 Months ago    Answers:  5   Viewed   40 times

I would like to parse data from JSON which is of type String. I am using Google Gson.

I have:

jsonLine = "
{
 "data": {
  "translations": [
   {
    "translatedText": "Hello world"
   }
  ]
 }
}
";

and my class is:

public class JsonParsing{

   public void parse(String jsonLine) {

      // there I would like to get String "Hello world"

   }

}

 Answers

44

This is simple code to do it, I avoided all checks but this is the main idea.

 public String parse(String jsonLine) {
    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject  jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("data");
    JsonArray jarray = jobject.getAsJsonArray("translations");
    jobject = jarray.get(0).getAsJsonObject();
    String result = jobject.get("translatedText").getAsString();
    return result;
}

To make the use more generic - you will find that Gson's javadocs are pretty clear and helpful.

Tuesday, June 1, 2021
 
Kenny
answered 6 Months ago
16

use JsonParser; for example:

JsonParser parser = new JsonParser();
JsonObject o = parser.parse("{"a": "A"}").getAsJsonObject();
Wednesday, June 2, 2021
 
apokryfos
answered 6 Months ago
81

You just need to create a Java class structure that represents the data in your JSON. In order to do that, I suggest you to copy your JSON into this online JSON Viewer and you'll see the structure of your JSON much clearer...

Basically you need these classes (pseudo-code):

class Response
  Data data

class Data
  List<ID> id

class ID
  Stuff stuff
  List<List<Integer>> values
  String otherStuff

Note that attribute names in your classes must match the names of your JSON fields! You may add more attributes and classes according to your actual JSON structure... Also note that you need getters and setters for all your attributes!

Finally, you just need to parse the JSON into your Java class structure with:

Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);

And that's it! Now you can access all your data within the response object using the getters and setters...

For example, in order to access the first value 456, you'll need to do:

int value = response.getData().getId().get(0).getValues().get(0).get(1);
Wednesday, June 9, 2021
 
altexpape
answered 6 Months ago
15

First problem: your VolumeContainer needs to be:

public class VolumeContainer {
   public List<Volume> volumes;
}

it does not need to be static.

Second problem: your Volume class should be like this:

public class Volume {
  private String status; 
  private Boolean managed; 
  private String name; 
  private Support support; 
  private String storage_pool; 
  private String id; 
  private int size;
  private List<String> mapped_wwpns;

  public String getId(){return id;}
  public String getName(){return name;}
}

I defined a class named Support like this:

public class Support {
   private String status;
   private List<String> reasons;
}

Third problem: parsing, If response string contains your example data, simply parse like this:

Gson g = new Gson();
VolumeContainer vc = g.fromJson(response, VolumeContainer.class);

Fourth problem: get the map. Finally to get your HashMap, just do like this:

HashMap<String, String> hm = new HashMap<String,String>();
for(Volume v: vc.volumes){
  hm.put(v.getId(), v.getName());  
}
Wednesday, June 30, 2021
 
khaverim
answered 5 Months ago
22

You can create a primitive that will contain the String value and add it to the array:

JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive("value1");
jArray.add(element);
Tuesday, August 10, 2021
 
TV Nath
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share