org.json.JSONException:没有项的值

时间:2013-08-20 04:39:01

标签: php android mysql arrays

当我尝试使用Android应用程序从MySql数据库查看数据时,我得到No value for {"username":"sara"}{"username":"john"} JSONException

  08-20 04:26:39.396: W/System.err(4732): org.json.JSONException: No value for {"username":"sara"}{"username":"john"}
  08-20 04:26:39.497: W/System.err(4732):   at org.json.JSONObject.get(JSONObject.java:354)
  08-20 04:26:39.497: W/System.err(4732):   at org.json.JSONObject.getJSONArray(JSONObject.java:544)
  08-20 04:26:39.517: W/System.err(4732):   at com.example.phpapp.ViewData$MyTask.doInBackground(ViewData.java:69)
  08-20 04:26:39.577: W/System.err(4732):   at com.example.phpapp.ViewData$MyTask.doInBackground(ViewData.java:1)
  08-20 04:26:39.606: W/System.err(4732):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
  08-20 04:26:39.606: W/System.err(4732):   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  08-20 04:26:39.631: W/System.err(4732):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  08-20 04:26:39.636: W/System.err(4732):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  08-20 04:26:39.636: W/System.err(4732):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  08-20 04:26:39.636: W/System.err(4732):   at java.lang.Thread.run(Thread.java:856)

PHP代码

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("can't connect");
$db = mysql_select_db($dbname) or die("no such db name");
$query = "select username from users";
$result = mysql_query($query);
while($row=  mysql_fetch_array($result)){
    echo json_encode($row);
}

jsonArray

{"username":"sara"}{"username":"john"}

Android代码

HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

String str = "";
while((str = br.readLine()) != null){
    s.append(str+"\n");
    mystr = s.toString();
}   

JSONObject object = new JSONObject(mystr);
JSONArray Jarray = object.getJSONArray(mystr);
for (int i = 0; i < Jarray.length(); i++) {
    JSONObject Jasonobject = Jarray.getJSONObject(i);
    st = Jasonobject.getString("username");
    alist.add(str);
}

如何解决这个问题?感谢

2 个答案:

答案 0 :(得分:2)

问题出在这一行

 JSONArray Jarray = object.getJSONArray(mystr);

其中mystr={"username":"sara"}{"username":"john"}(来自输入流的数据)

所以在这里你试图从JSONObject mystr

访问索引为object的JSON数组

技术上从不存在,因此它提供JSONException: No value for {"username":"sara"}{"username":"john"}

<强>解决方案

使用JJPA的答案

来纠正PHP代码

使用

JSONArray Jarray=new JSONArray(mystr);

答案 1 :(得分:1)

这个

{"username":"sara"}{"username":"john"}

不是json数组,甚至不是有效的json字符串。

如果这是一个json数组,那么它将如下所示,

[{"username":"sara"},{"username":"john"}]

所以你需要在这些对象之间加一个逗号,FYI json对象将与{}对齐,数组将在[]。因此,您需要更正json字符串,然后为其编写json解析器。

你的php代码应该修改如下,打印json数组,

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("can't connect");
$db = mysql_select_db($dbname) or die("no such db name");
$query = mysql_query("select username from users");
$return_arr = array();

while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
    $row_array['id'] = $row['id'];
    $row_array['col1'] = $row['col1'];// and your respective cols
    array_push($return_arr,$row_array);
}

echo json_encode($return_arr);