org.json.JSONException:字符 26 处的未终止对象.在安卓中



我正在返回一个我正在尝试解析的 JSON 对象,我以前从未遇到过这个问题。 我已经尝试了一些SO答案(尝试"尾随\"),但是似乎没有一个有效。

杰森

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

使用 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);

最新更新