2016-05-23 59 views
0

我回來了一個JSON對象,我試圖解析,我從來沒有遇到過這個問題。我已經嘗試了一些所以的答案(嘗試'尾隨'),但是沒有一個可以工作。org.json.JSONException:未終止的對象在字符26的..在Android中

JSON

{ 
    "country": "United States", 
    "mainUrl": "http://www.espn.com", 
    "outlets": [ 
    { 
     "country": "U.S.A.", 
     "displayName": "ESPN", 
     "sourceUrl": "http://www.espn.com" 
    }, 
    { 
     "country": "U.S.A.", 
     "displayName": "CNN", 
     "sourceUrl": "http://www.cnn.com" 
    }, 
    { 
     "country": "U.S.A.", 
     "displayName": "Fox News", 
     "sourceUrl": "http://www.foxnews.com" 
    }, 
    { 
     "country": "U.S.A.", 
     "displayName": "Comcast Sports Network", 
     "sourceUrl": "http://www.csn.com" 
    }, 
    { 
     "country": "U.S.A.", 
     "displayName": "Yahoo", 
     "sourceUrl": "http://www.yahoo.com" 
    }, 
    { 
     "country": "U.S.A.", 
     "displayName": "Google News", 
     "sourceUrl": "http://www.googlenews.com" 
    } 
    ] 
} 

方法來獲得JSON

ParseQuery<Configuration> query = ParseQuery.getQuery(Configuration.class); 
      query.whereEqualTo("packageName", "mypackagename"); 
      query.findInBackground(new FindCallback<Configuration>() { 
       public void done(List<Configuration> configuration, ParseException e) { 
        if (e == null) { 
         try { 

          JSONObject object = new JSONObject(String.valueOf(configuration.get(0).get("appConfig"))); 

          url = object.getString("mainUrl"); 

          Log.d("thisstuff", url); 

         } catch (JSONException e1) { 
          e1.printStackTrace(); 
         } 

        } else { 
         Log.d("score", "Error: " + e.getMessage()); 
        } 
       } 
      }); 
+0

解析響應爲Response.class附上一個[MCVE爲了證明這個問題。 – t0mm13b

+0

寫你的代碼描述你的問題 –

+0

很抱歉更新。我檢查配置的大小,以確保我得到了一排。我是誰。 – AndroidDev21921

回答

1

使用GSON解析JSON響應。你應該這樣創建POJO:

public class Outlet { 

@SerializedName("country") 
@Expose 
private String country; 
@SerializedName("displayName") 
@Expose 
private String displayName; 
@SerializedName("sourceUrl") 
@Expose 
private String sourceUrl; 

/** 
* 
* @return 
* The country 
*/ 
public String getCountry() { 
return country; 
} 

/** 
* 
* @param country 
* The country 
*/ 
public void setCountry(String country) { 
this.country = country; 
} 

/** 
* 
* @return 
* The displayName 
*/ 
public String getDisplayName() { 
return displayName; 
} 

/** 
* 
* @param displayName 
* The displayName 
*/ 
public void setDisplayName(String displayName) { 
this.displayName = displayName; 
} 

/** 
* 
* @return 
* The sourceUrl 
*/ 
public String getSourceUrl() { 
return sourceUrl; 
} 

/** 
* 
* @param sourceUrl 
* The sourceUrl 
*/ 
public void setSourceUrl(String sourceUrl) { 
this.sourceUrl = sourceUrl; 
} 

} 

public class Respose { 

@SerializedName("country") 
@Expose 
private String country; 
@SerializedName("mainUrl") 
@Expose 
private String mainUrl; 
@SerializedName("outlets") 
@Expose 
private List<Outlet> outlets = new ArrayList<Outlet>(); 

/** 
* 
* @return 
* The country 
*/ 
public String getCountry() { 
return country; 
} 

/** 
* 
* @param country 
* The country 
*/ 
public void setCountry(String country) { 
this.country = country; 
} 

/** 
* 
* @return 
* The mainUrl 
*/ 
public String getMainUrl() { 
return mainUrl; 
} 

/** 
* 
* @param mainUrl 
* The mainUrl 
*/ 
public void setMainUrl(String mainUrl) { 
this.mainUrl = mainUrl; 
} 

/** 
* 
* @return 
* The outlets 
*/ 
public List<Outlet> getOutlets() { 
return outlets; 
} 

/** 
* 
* @param outlets 
* The outlets 
*/ 
public void setOutlets(List<Outlet> outlets) { 
this.outlets = outlets; 
} 

} 

然後你就可以使用此代碼

Gson gson = new Gson(); 

Response response = gson.fromJson(json, Response.class); 
相關問題