php - 在 php 中创建 JSON 数组并在 android 中对其进行解码。组织.json.JSONException : No value for id

标签 php android json

我使用下面的代码在 php 中创建 json 数组

$jsonData      = array();
$jsonTempData  = array();
$count=0;
$result = $link->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $jsonTempData['id']=$row['_id'];
        $jsonTempData['title']=$row['title'];

        $jsonData[$count++]=json_encode($jsonTempData);

    }
} 
$link->close();
$outputArr = array();

$outputArr['Android']=$jsonData;

print_r( json_encode($outputArr));

并通过以下代码在android中解码

 jsonResponse = new JSONObject(Content);
JSONArray array = jsonResponse.optJSONArray("Android");

for(int i=0;i<array.length();i++){
    nm.set_id(array.getJSONObject(i).getInt(DBHelper.ID));

    nm.setTitle(array.getJSONObject(i).getString(DBHelper.TITLE));
}

但是我在android中得到了这个异常

: org.json.JSONException: No value for id

问题是什么? 如何解决这个问题?

我的目标是在 php 中创建二维数组并将其转换为 JSON 对象数组以在 android 中提取。

最佳答案

问题是由于 array 的 JSON Parsing Exception 引起的

JSONException:类型 java.lang.String 的值无法转换为 JSONObject

修复:

我没有直接将数组更改为 JSON 对象,而是先将数组项更改为字符串,然后再将其更改为 JSON 对象

在下面的代码中,我已将您的代码更改为可以工作:

 jsonResponse = new JSONObject(Content);

 JSONArray array = jsonResponse.optJSONArray("Android");

for(int i=0;i<array.length();i++){

    String strJSON=array.getString(i);

    JSONObject jsonObj = new JSONObject(strJSON);   

    nm.set_id(jsonObj.getInt(DBHelper.ID));

    nm.setTitle(jsonObj.getString(DBHelper.TITLE));
}

这是我试过的完整示例,以备不时之需

php部分(test.php)

 <?php
header('Content-type=application/json; charset=utf-8');
$jsonData      = array();
$jsonTempData  = array();

$jsonTempData['id']="id1";
$jsonTempData['title']="title1";

$jsonData[0]=json_encode($jsonTempData);

$jsonTempData['id']="id2";
$jsonTempData['title']="title2";

$jsonData[1]=json_encode($jsonTempData);

$outputArr = array();

$outputArr['Android']=$jsonData;

print_r(json_encode($outputArr));

?>

结果 JSON:

["{\"id\":\"id1\",\"title\":\"title1\"}","{\"id\":\"id2\",\"title\":\"title2\"}"]

安卓部分:

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpGet req = new HttpGet("http://localhost/test.php");

HttpResponse response;
try {
     response = httpClient.execute(req);
    if (response.getStatusLine().getStatusCode() == 200) {

                                        String result;
                                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
                                        StringBuilder sb = new StringBuilder();

                                        String line = null;
                                        while ((line = bufferedReader.readLine()) != null) {
                                        sb.append(line + "\n");
                                        }
                                        result = sb.toString();
                                        Log.d("result:",result.toString());

                                        JSONObject jsonResponse = new JSONObject(result);
                                         JSONArray array = jsonResponse.optJSONArray("Android");

                                         for(int i=0;i<array.length();i++){
                                             String strJSONobj=array.getString(i);
                                             JSONObject json = new JSONObject(strJSONobj);                                           
                                             Log.d("result",json.getString("id"));
                                             Log.d("result",json.getString("title"));                                       
                                         }


                                        }
                                } catch (ClientProtocolException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                } // ClientProtocolException

希望对你有帮助

关于php - 在 php 中创建 JSON 数组并在 android 中对其进行解码。组织.json.JSONException : No value for id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797196/

相关文章:

java - 无法在android drawable xml中添加属性原色

Javascript JSON.stringify 函数不起作用

php - 从 codeigniter 中的文件夹加载模型

php - 统计MySQL中不为空的字段个数

php - Laravel orwhere 和 whereBetween 查询不能一起工作

json - 在 golang 中像请求一样发送 curl

ios - 如何遍历 JSON 坐标并将注释构建为一个函数?

php - 预约确定

android - 从图库 View 中选择多个图像

android - PhoneStateListener onSignalStrengthsChanged 停止在服务中被调用