Gson只能从JSON中正确解析一个对象吗?

时间:2017-05-30 15:46:01

标签: java json parsing gson inputstream

我有以下JSON:

{
  "Person": {

     "id": "1",
     "name": "sampleName"

  },
  "PersonCalender ": {
    "start": "2017-01-25T19:00:00+0100",
    "End": "2019-05-10T19:00:00+0100"

  }
}

这是它对应的Java对象(包含2个对象):

public class PersonRequest {
    private Person person;
    private PersonCalender personCalender;

    //getters and setters

    }

下面显示了我如何尝试parse对象,但只有Person对象被正确解析。

我是否犯了错误或使用JSONGson无效解析为此对象?

Gson Parsing:

 PersonRequest personRequest = new PersonRequest();
        try {
            InputStream is = PersonTest.class.getResourceAsStream("/my/path/personRequest.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

            Gson gson = new Gson();
            personRequest = gson.fromJson(bufferedReader, PersonRequest.class);

        } catch (Exception e) {
            logger.logMessage("Exception: " + e);
        }

1 个答案:

答案 0 :(得分:1)

这里有两个错误。 1 - MAX之后的空格,2- PersonCalender中的第一个字母应为小写(根据您的java代码)