简体   繁体   English

如何使用GSON库将json文件读入java

[英]How to read json file into java with GSON library

I want to read this JSON file with java using GSON library. 我想使用GSON库用java读取这个JSON文件。 I am new to using gson libray. 我是新手使用gson libray。 someone please correct my code My JSON file looks like this: 有人请更正我的代码我的JSON文件如下所示:

{  
"tableRows":[  
  {  
     "id":"100",
     "mailContent":"Test mail content 123",
     "sentiment":"0"
  },
  {  
     "id":"200",
     "mailContent":"Test mail content 123",
     "sentiment":"0"
  },
  {  
     "id":"300",
     "mailContent":"Test mail content 123",
     "sentiment":"0"
  }
 ]
}

This is the java code I wrote to read this file: 这是我为阅读此文件而编写的java代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonObject;
public class Sample {
 public static void main(String[] args) {

     JsonParser parser = new JsonParser();
     try { 
        JsonElement jsontree = parser.parse(
            new FileReader(
                "/Users/kesavan-4688/Desktop/JSP-Eclipse/Sample/src/Demo/sample.json"
            )
        );
        JsonElement je = jsontree.getAsJsonObject();
        JsonArray ja = je.getAsJsonArray();
        for (Object o : ja)
        {
            JsonObject person = (JsonObject) o;

            String id = person.get("id").getAsString();
            System.out.println(id);

            String mail = person.get("mailcontent").getAsString();
            System.out.println(mail);

            String sentiment = person.get("sentiment").getAsString();
            System.out.println(sentiment);
        }   
     }  
     catch (JsonIOException e) {
        e.printStackTrace();
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
 }

 }

But I get the following exception: 但我得到以下异常:

Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array.
at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
at Demo.Sample.main(Sample.java:18)

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: 您尝试将JsonObject转换为无法工作的JsonArray ,您需要先获取根JsonObject然后使用getAsJsonArray(String memberName)将属性tableRows作为JsonArray作为下一个:

...
// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM