Parse JSON File Using Gson

how to parse JSON file with GSON

You have to fetch the whole data in the list and then do the iteration as it is a file and will become inefficient otherwise.

private static final Type REVIEW_TYPE = new TypeToken<List<Review>>() {
}.getType();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(filename));
List<Review> data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole reviews list
data.toScreen(); // prints to screen some values

Parse JSON file using GSON

Imo, the best way to parse your JSON response with GSON would be creating classes that "match" your response and then use Gson.fromJson() method.

For example:

class Response {
Map<String, App> descriptor;
// standard getters & setters...
}

class App {
String name;
int age;
String[] messages;
// standard getters & setters...
}

Then just use:

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

Where yourJson can be a String, any Reader, a JsonReader or a JsonElement.

Finally, if you want to access any particular field, you just have to do:

String name = response.getDescriptor().get("app3").getName();

You can always parse the JSON manually as suggested in other answers, but personally I think this approach is clearer, more maintainable in long term and it fits better with the whole idea of JSON.

How to read json file into java with GSON library

You try to convert a JsonObject to a JsonArray which cannot work, you need fist to get the root JsonObject then use getAsJsonArray(String memberName) to get the property tableRows as JsonArray as next:

...
// Get the root JsonObject
JsonObject je = jsontree.getAsJsonObject();
// Get the property tableRows as a JsonArray
JsonArray ja = je.getAsJsonArray("tableRows");
for (Object o : ja) {
...
// Warning JSON is case sensitive so use mailContent instead of mailcontent
String mail = person.get("mailContent").getAsString();
...
}

Output:

100
Test mail content 123
0
200
Test mail content 123
0
300
Test mail content 123
0

How do I parse a json file using gson?

JSON is supposed to be encoded in UTF-8. FileReader assumes the file is encoded in the default encoding for text files for your system which might not be UTF-8. If you use Files.newBufferedReader you can speicify the encoding:

try (Reader reader = Files.newBufferedReader(Paths.get("./input/raw-1.json"), StandardCharsets.UTF_8)) {
Items[] myItems = gson.fromJson(reader, Items[].class);
}

Likewise, you should specify the encoding when writing:

try (Writer writer = Files.newBufferedWriter(Paths.get("./output/output-sample-test.json", StandardCharsets.UTF_8)) {
gson2.toJson(myItems, writer);
}

Parsing JSON File using Gson

This is the working code which is running perfectly in my system:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class EntityName {

public static void main(String[] args) throws JsonIOException, JsonSyntaxException, FileNotFoundException {

File f = new File("others//entity-name.json");
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(new FileReader(f));
JsonElement element = jsonElement.getAsJsonObject().getAsJsonObject("services").get("M");
System.out.println(element.getAsInt());
}

}

Here entity-name.json contains given json.

Parsing a JSON file in Java using GSON

Group class contains List<Student>, again not required to add [] at the end of the group variable name.

  public class Group {

private List<Student> group;

public void setStudents(List<Student> students) {
this.group = students;
}

public List<Student> getStudents() {
return group;
}

@Override
public String toString() {
return "Group [group=" + group + "]";
}
}

make sure that field names in the classes need to match the JSON.

public class Student {
private String name;
private String email; //sfu_email changed to email

public Student(String name, String email) {
this.name = name;
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "Student [name=" + name + ", email=" + email + "]";
}

try with the above solution.

Using GSON to parse Json into a JAVA Object where the Json Elements may vary

I'm not sure if I understood your question right. As per my understanding, you are trying to convert a json object with an extra field which is not available in the java class. Frankly, I don't understand why you want that or if it's possible to start with. You can have a workaround by converting the json to Map.

Map map = gson.fromJson(jsonString, Map.class);



Related Topics



Leave a reply



Submit