我有一个Json数据,我从wordpress博客获得,当我尝试传递url时,我在logcat中收到此错误,说明 org.json.JSONException:没有评论值。 < / p>
我想要做的是从wordpress博客中检索提交。
我已粘贴完整代码here以供进一步参考,
03-13 11:43:59.698: W/System.err(18926): org.json.JSONException: No value for comments
03-13 11:43:59.698: W/System.err(18926): at org.json.JSONObject.get(JSONObject.java:354)
03-13 11:43:59.698: W/System.err(18926): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
03-13 11:43:59.698: W/System.err(18926): at com.cepfmobileapp.org.CommentActivityWp$GetQuery.doInBackground(CommentActivityWp.java:271)
03-13 11:43:59.698: W/System.err(18926): at com.cepfmobileapp.org.CommentActivityWp$GetQuery.doInBackground(CommentActivityWp.java:1)
03-13 11:43:59.698: W/System.err(18926): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 11:43:59.698: W/System.err(18926): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 11:43:59.698: W/System.err(18926): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 11:43:59.708: W/System.err(18926): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-13 11:43:59.708: W/System.err(18926): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 11:43:59.708: W/System.err(18926): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 11:43:59.708: W/System.err(18926): at java.lang.Thread.run(Thread.java:856)
Java代码
private static final String TAG_COMMENTS = "comments";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_CONTENT = "content";
Intent i = getIntent();
// Receiving the Data
ida = i.getStringExtra("id");
url = "" + ida;
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
comments = jsonObj.getJSONArray(TAG_COMMENTS);
// looping through All Contacts
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
// Phone node is JSON Object
//JSONObject comments = c.getJSONObject(TAG_COMMENTS );
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String content = c.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
答案 0 :(得分:4)
Ty this ..
您的回复中的JSONArray中没有comments
,您必须与jsonObj.has(TAG_CONTENT)
检查TAG名称是否存在。
JSONObject jsonpost= jsonObj.getJSONObject("post");
if(jsonpost.has(TAG_ID)){
comments = jsonpost.getJSONArray(TAG_COMMENTS);
for (int i = 0; i < comments.length(); i++) {
String id = "";
String name = "";
String content = "";
if(c.has(TAG_ID))
id = c.getString(TAG_ID);
if(c.has(TAG_NAME))
name = c.getString(TAG_NAME);
if(c.has(TAG_CONTENT))
content = c.getString(TAG_CONTENT);
}
}
答案 1 :(得分:1)
试试如下:
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
comments = jsonObj.getJSONArray("posts");
// looping through All Contacts
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
//get the comments arraylist
JSONArray commentsdata = c.getJSONArray(TAG_COMMENTS);
for (int j = 0; j < commentsdata.length(); j++)
{
JSONObject comntObj = commentsdata.getJSONObject(j);
String name = comntObj.getString("name");
String sUrl = comntObj.getString("url");
String scontent = comntObj.getString("content");
....................
}
String id = c.getString(TAG_ID);
String content = c.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONObject jsonpost= jsonObj.getJSONObject("post");
// looping through All Contacts
for (int i = 0; i < jsonpost.length(); i++) {
JSONObject c = jsonpost.getJSONObject(i);
JsonArray comments=c.getJSONArray("comments");
for (int j = 0; j< comments.size();j++) {
JSONObject jj = comments.getJSONObject(j);
String id = jj.getString(TAG_ID);
String name = jj.getString(TAG_NAME);
String content = jj.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
}
我希望它对你有用..
答案 3 :(得分:0)
try{
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject jsonpost= jsonObj.getJSONObject("post");
comments = jsonpost.getJSONArray("comments");
...........................................
//your rest codes//
}
您的comments
是post
JSONObject内的JSONArray。因此,您需要从comments
JSONObject访问post
。