Java 在使用JsonArray将对象存储在RecyclerView中之后,如何解析Android JsonObject并将其存储
我试图解析一个Java 在使用JsonArray将对象存储在RecyclerView中之后,如何解析Android JsonObject并将其存储,java,android,arrays,json,Java,Android,Arrays,Json,我试图解析一个Json,如下所示: { "status": "ok", "feed": { "title": "title1", "link": "http://www.link/", "author": "", "description": "desc", "image": "" }, "items": [ { "titl
Json
,如下所示:
{
"status": "ok",
"feed": {
"title": "title1",
"link": "http://www.link/",
"author": "",
"description": "desc",
"image": ""
},
"items": [
{
"title": "something",
"link": "something",
"guid": "something",
"pubDate": "date",
"categories": [],
"author": "",
"thumbnail": "",
"description": null,
"content": null
},
...
...
我从响应中得到了预期的结果,但这不是重点。我所做的如下:
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
item.setTitle(post.getString("title"));
item.setDate(post.getString("pubDate")); // As expected results
feedItem.add(item);
}
}
第二次尝试:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.io.StringReader.<init>(StringReader.java:47)
at android.text.HtmlToSpannedConverter.convert(Html.java:449)
at android.text.Html.fromHtml(Html.java:136)
at android.text.Html.fromHtml(Html.java:99)
at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:34)
at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:14)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1684)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1139)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:810)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.vi
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
PostItems item2 = new PostItems();
item2.setTitle(post.getString("title"));
item2.setDate(post.getString("pubDate"));
feedItem.add(item2);
}
feedItem.add(item);
}
试试看{
JSONObject响应=新JSONObject(结果);
JSONObject提要_object=response.getJSONObject(“提要”);
positems item=新positems();
item.setAuthor(feed_object.getString(“title”);
JSONArray items_arr=response.getJSONArray(“items”);
对于(int i=0;i
事实上,我尝试过任何你能想象得到的方法,看到了stackoverflow上的任何问题,我找不到类似的问题,但有一次,只显示了一个项目,没有来自对象的正确数据,有一次没有显示来自数组的正确数据
任何帮助都会很好。您试图在每个项目中使用getAuthor()
,但显然在ArrayList
中只为一个(第一个)项目设置了它。当它没有任何值时,它返回null
,结果是NullPointerException
。此外,还应在每次迭代中创建新对象
快走
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
用于循环的内部
编辑
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title")); // being fetched from top node
item.setTitle(post.getString("title")); // being fetched from array
item.setDate(post.getString("pubDate")); // being fetched from array
feedItem.add(item);
}
}
试试看{
JSONObject响应=新JSONObject(结果);
JSONObject提要_object=response.getJSONObject(“提要”);
JSONArray items_arr=response.getJSONArray(“items”);
对于(int i=0;i
行是什么MyRecyclerAdapter.java:34
?就是它,feedListRowHolder.author.setText(Html.fromHtml(feedItem.getAuthor())代码>但我认为这不是重点,只有当我像第二个代码一样尝试两次设置此行时才会发生这种情况:feedItem.add(item2);添加(项目)代码>getAuthor
返回空值。侧面注释:positems item=new positems()
应该在for循环的每一次迭代中被实例化——Blackbelt刚才我也这么认为,但是请继续显示上面的结果(错误),您并不是在代码的任何地方解析作者。为什么您希望getAuthor返回某些内容!=空?不起作用,这不是我说的,请阅读注释。请将您的代码与我的代码匹配。试试这段代码。太好了,关键是在每个循环中使用这些代码,不要想它。当然,不要想它:),我大约花了一天的时间在上面!谢谢