2013-07-25 44 views
4

我有以下結果類,其對象將作爲JSON返回。GSON - JsonSyntaxException - 第7行第4列的預期名稱

public class Result { 
    public String objectid; 
    public String dtype; 
    public String type; 
    public String name; 
    public String description; 

    public Result() { 
     this.objectid = ""; 
     this.dtype= ""; 
     this.type=""; 
     this.name= ""; 
     this.description = ""; 
    } 

    public String getObjectid() { 
     return objectid; 
    } 

    public void setObjectid(String s) { 
     this.objectid = s; 
    } 

    public String getDtype() { 
     return dtype; 
    } 

    public void setDtype(String s) { 
     this.dtype= s; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String s) { 
     this.type = s; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String s) { 
     this.description = s; 
    } 

} 

我有一個配置json,它讀取我的主要.java並作爲json返回爲HTTP RESPONSE。這是如下:

{ 
     "objectid" : "test", 
     "dtype" : "test", 
     "type" : "test", 
     "name" : "test", 
     "description" : "test" // removed 
}, 
{ 
     "objectid" : "test", 
     "dtype" : "test", 
     "type" : "test", 
     "name" : "test", 
     "description" : "test" 
} 

主要的.java

使用GSON,它會讀取configuration.json文件,必須返回一個JSON。

我的代碼:

Gson g = new Gson(); 
Result r = g.fromJson(res.toString(), Result.class); 

其中res.toString()得到我的configuration.json文件內容爲字符串。

我的問題:

我遇到以下異常:

異常com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(真)在第7行柱接受畸形JSON 3
com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(真),以在第7行第3列

接受JSON格式錯誤

任何指針?

+0

爲什麼在「名稱」中有間隔?你的錯誤說你有壞JSON – CBIII

+0

@ClydeByrdIII我修好了,對不起。仍然是確切的問題。 – Namenoobie

+0

您是否試圖從該Json解析多個'Result's?如果是這樣,你需要你的Json看起來像一個列表,並告訴Gson它也是一個列表。 –

回答

9

如果這是實際的json:這裏有一個額外的逗號和一個拼寫錯誤。錯誤說你有不好的json語法。所以這可能是首先看的地方之一。

{ 
      "objectid" : "test", 
      "dtype" : "test", 
      "type" : "test", 
      "name " : "test", 
      "description" : "test", //delete this comma 
      }, 
      { 
      "objectid" : "test", 
      "dtyoe" : "test", // spelling error 
      "type" : "test", 
      "name " : "test", 
      "description" : "test" 
    } 

你似乎也在解析兩個對象,並告訴gson你想從它得到一個結果對象。 考慮無論是單獨解析的對象或者告訴GSON你想要一個結果數組的返回

+0

我不明白。我沒有任何額外的開口花括號。 – Namenoobie

+0

所以有兩個對象,逗號分開,在configuration.json – Namenoobie

+0

裏面對不起,我認爲它是對象中的對象,我改變了我的答案。到後面的逗號你有 – CBIII

3

使用

catch(JsonSyntaxException e) 

代替

catch(MalformedJsonException e) 

因爲MalformedJsonException是一些內部異常而JsonSyntaxException是一個實際被拋出。這裏是一個代碼片段

  String response="Something"; 
      JsonElement my_json; 
      try { 
       my_json=jsonParser.parse(response); 
      } catch(JsonSyntaxException e) { 
       e.printStackTrace(); 
       JsonReader reader = new JsonReader(new StringReader(response)); 
       reader.setLenient(true); 
       my_json=jsonParser.parse(reader); 
      } 
0

你有兩個對象,但從GSON獲取一個對象。要麼使用以下格式的JSON字符串從此JSON字符串中獲取Result對象。

{ 
    "objectid" : "test", 
    "dtype" : "test", 
    "type" : "test", 
    "name" : "test", 
    "description" : "test" 

}

第二個選項: -

你將不得不改變JSON字符串格式。你將不得不在JSON數組中改變它並獲得這個對象的ArrayList,之後,我們可以使用數組列表index.new得到Result對象。新的JSON字符串就像這樣。

{"resultArray":[{ 
    "objectid" : "test", 
    "dtype" : "test", 
    "type" : "test", 
    "name" : "test", 
    "description" : "test" 
      }, 
     { 
    "objectid" : "test", 
    "dtype" : "test", 
    "type" : "test", 
    "name" : "test", 
    "description" : "test" 
     }]} 

和java代碼來獲取結果對象。

Gson g = new Gson(); 
ResultList r = g.fromJson(res.toString(), ResultList.class); 
ArrayList<Result> resultList = r.getResultList(); 
     for(int x=0 ;x<resultList.size();x++){ 
       Result result = resultList.get(x); 
       } 

ResultList模型是: -

public class ResultList { 
@SerializedName("resultArray") 
public ArrayList<Result> resultList; 
public getResultList(){ 
      return resultList; 
      } 
     } 

@Note: - 如上文給出Result.java將用於相同。

相關問題