2013-07-25 4 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을 반환해야한다.

내 코드 : res.toString() 나에게 문자열로 configuration.json 파일 내용을 얻을 수

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

.

내 문제 :

나는 다음과 같은 예외가 발생하는 오전 :

예외를 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 구문이 잘못되었다고 말합니다. 그래서 이것은 아마도 처음으로보아야 할 곳 중 하나입니다. 당신이 그것에서 하나의 결과 개체를 원하는 GSON

{ 
      "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" 
    } 

는 또한 두 개체를 구문 분석하고 말하는 것 같다. MalformedJsonException 일부 내부 예외이기 때문에 JsonSyntaxException가 실제로 일 동안 하나가 개별적으로 개체를 구문 분석을 고려하거나 돌아 가기

+0

나는 이해하지 못한다. 거기에 여분의 중괄호가 없습니다. – Namenoobie

+0

그래서 쉼표로 분리 된 두 개의 객체가 configuration.json 내에 있습니다. – Namenoobie

+0

미안 해요. 객체의 객체라고 생각했습니다. 제 대답이 바뀌 었습니다. 해당 후행 쉼표로 – CBIII

3

사용

catch(JsonSyntaxException e) 

대신

catch(MalformedJsonException e) 

의 결과 배열을 원하는 GSON 이야기 던져 버려. 여기 코드 스 니펫이 있습니다

  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 JSON 문자열을 사용하여 Result 객체를 얻을 수 있습니다.

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

결과 개체를 가져 오는 코드입니다.

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이 같은 사용됩니다.

관련 문제