org.json.JSONException:Android中第26个字符的未终止对象

时间:2016-05-23 14:43:33

标签: android json

我正在找回一个我想解析的JSON对象,我之前从未遇到过这个问题。我已经尝试了一些SO答案(尝试'尾随'),但似乎没有一个工作。

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());
                    }
                }
            });

1 个答案:

答案 0 :(得分: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;
}

}

然后,您可以使用此代码

将响应解析为Response.class
Gson gson = new Gson();

Response response = gson.fromJson(json, Response.class);