JSON到Gson-无法将值转换为JSONObject7

时间:2018-12-08 08:44:49

标签: android gson activeandroid

我正在根据Database创建JSON。运行应用程序时出现此错误:  解析新闻JSON结果时出现问题     org.json.JSONException:类型的java.lang.String值特朗普不能转换为JSONObject

以下是代码:

private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty( newsJSON )) {
        return null;
    }

    List<News> newsall = new ArrayList<>();

    try {
        JSONObject data = new JSONObject(newsJSON);
        JSONArray results = data.getJSONArray("articles");
        for (int i = 0; i < results.length(); i++) {
            JSONObject obj = results.getJSONObject(i);
            String webTitle = obj.getString("title");
            String webUrl = obj.getString("url");
            String webImage = obj.getString( "urlToImage" );

            List<NewsForDB> newsForDB = new ArrayList<>(  );
            Gson gson = new Gson();
            NewsForDB webTitleForDB=gson.fromJson( extractFeatureFromJson( webTitle ).toString(), NewsForDB.class );
            newsForDB.add(webTitleForDB);
            NewsForDB urlForDB=gson.fromJson( extractFeatureFromJson( webUrl ).toString(), NewsForDB.class );
            newsForDB.add(urlForDB);
            NewsForDB webImageForDB=gson.fromJson( extractFeatureFromJson( webImage ).toString(), NewsForDB.class );
            newsForDB.add( webImageForDB );


            News news = new News(webTitle, webUrl, webImage);
            newsall.add(news);

        }



    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return newsall;
}

如果没有这行代码

List<NewsForDB> newsForDB = new ArrayList<>(  );
        Gson gson = new Gson();
        NewsForDB webTitleForDB=gson.fromJson( extractFeatureFromJson( webTitle ).toString(), NewsForDB.class );
        newsForDB.add(webTitleForDB);
        NewsForDB urlForDB=gson.fromJson( extractFeatureFromJson( webUrl ).toString(), NewsForDB.class );
        newsForDB.add(urlForDB);
        NewsForDB webImageForDB=gson.fromJson( extractFeatureFromJson( webImage ).toString(), NewsForDB.class );
        newsForDB.add( webImageForDB );

一切正常,但我正在尝试准备使用ActiveAndroid ORM.

的代码

有什么建议吗?

谢谢!

更新:

我正在跟踪here的第一个答案。

新闻:

public class News {
private String mWebTitle;
private String mUrl;
private String mImage;

public News(String webTitle, String webUrl, String webImage) {
    mWebTitle = webTitle;
    mUrl = webUrl;
    mImage = webImage;

}

public String getWebTitle() {
    return mWebTitle;
}
public String getUrl() {
    return mUrl;
}
public String getWebImage() {return mImage;}

}

这是QueryUtils的代码的一部分,该代码在第一个问题中发布之前

public static List<News> fetchNewsData(String requestUrl) {
    URL url = createUrl( requestUrl );

    String jsonResponse = null;
    try {
        jsonResponse = makeHttpRequest( url );
    } catch (IOException e) {
        Log.e( LOG_TAG, "Problem making the HTTP request.", e );
    }

    List<News> news = extractFeatureFromJson( jsonResponse );

    return news;
}

这是NewsForDB.java

    import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;


@Table(name = "NewsDB")

public class NewsForDB extends Model {

    @Column(name = "title", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public String title;

    @Column(name = "url")
    public String url;

    @Column(name = "urlToImage")
    public String urlToImage;

    public NewsForDB(){
        super();
    }

    public NewsForDB(String title, String url, String urlToImage){
        super();
        this.title = title;
        this.url = url;
        this.urlToImage = urlToImage;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为当您在调用extractFeatureFromJson时在函数extractFeatureFromJson( webTitle )中进行递归操作时会发生问题,等等。

也许您应该共享您的类NewsNewsForDB,成为例外的测试用例,并且您能告诉我创建对象newsForDB而不使用的目的吗?

答案 1 :(得分:0)

您可以像这样编辑代码!

private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty( newsJSON )) {
        return null;
    }

    List<News> newsall = new ArrayList<>();

    try {
        JSONObject data = new JSONObject(newsJSON);
        JSONArray results = data.getJSONArray("articles");
        for (int i = 0; i < results.length(); i++) {
            Gson gson = new Gson();
            JSONObject obj = results.getJSONObject(i);
            News news = gson.fromJson(obj.toString() , News.class);
            newsall.add(news);
        }
    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return newsall;
}