How to parse with GSON when identifier has space in name

前端 未结 2 1412
迷失自我
迷失自我 2020-12-03 15:49

How to parse with GSON when file looks like this

{
    \"Person Id\":\"test\",
    \"Last Name\": \"test\",
    \"First Name\":\"test\"
}

2条回答
  •  抹茶落季
    2020-12-03 16:08

    I have tried to parse this JSON but I didn't use GSON to parse this. I'll share my code with you, kindly consider only as a supplementary solution to solve the issue:

    String parse = "{\"Person Id\":\"test\",\"Last Name\": \"lname\",\"First Name\":\"fname\"}";
    try {
        JSONObject jsonObject   =   new JSONObject(parse);
        String id = jsonObject.getString("Person Id");
        System.out.println("id="+id);
        System.out.println("lname="+jsonObject.getString("Last Name"));
        System.out.println("fname="+jsonObject.getString("First Name"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题