JSON 到 Gson - 值无法转换为 JSONObject7
JSON to Gson - Value cannot be converted to JSONObject7
我正在从 JSON
创建 Database
。 运行 应用程序时出现此错误:
解析新闻 JSON
结果时出现问题
org.json.JSONException:
java.lang.String
类型的值 Trump 无法转换为 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.
有什么建议吗?
谢谢!
更新:
我正在关注 的第一个答案。
新闻:
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;
}
}
谢谢!
我认为当您调用 extractFeatureFromJson( webTitle )
时在函数 extractFeatureFromJson
中进行递归时会出现问题,依此类推。
也许你应该分享你的 class News
, NewsForDB
, 异常的测试用例,你能告诉我你创建一个没有使用的对象 newsForDB
的目的吗?
您可以像这样编辑代码!
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;
}
我正在从 JSON
创建 Database
。 运行 应用程序时出现此错误:
解析新闻 JSON
结果时出现问题
org.json.JSONException:
java.lang.String
类型的值 Trump 无法转换为 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.
有什么建议吗?
谢谢!
更新:
我正在关注
新闻:
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;
}
}
谢谢!
我认为当您调用 extractFeatureFromJson( webTitle )
时在函数 extractFeatureFromJson
中进行递归时会出现问题,依此类推。
也许你应该分享你的 class News
, NewsForDB
, 异常的测试用例,你能告诉我你创建一个没有使用的对象 newsForDB
的目的吗?
您可以像这样编辑代码!
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;
}