JSONObject 无法转换为 JSONArray,但要成功测试



我收到此错误:JSONObject cannot be converted to JSONArray

由这部分代码引起:

private int parse() {
        try {
            Log.d("Jou", "result");
            JSONArray ja = new JSONArray(data);
            JSONObject jo = null;
            titles.clear();
            skills.clear();
            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);
                String id = jo.getString("ID");
                String title = jo.getString("post_title");
                //String content = jo.getString("post_content");
                String date = jo.getString("post_date");
                Skill skill = new Skill();
                skill.setId(id);
                skill.setTitle(title);
                //skill.setContent(content);
                skill.setDate(date);
                skills.add(skill);
                titles.add(title);
            }
            return 1;
        } catch (JSONException e) {
            Log.d("Jou", e.getMessage());
            return 0;
        }

虽然我之前试过并且完全相同,但我添加了另一个字符串,它是我得到错误的日期。代码可能有什么问题?

这是需要解析的服务器的结果:

s = {"result":[{"post_id":"390","post_title":"Cart","post_date":‌​"2017-02-07 12:17:29"},{"post_id":"421","post_title":"Front End Developer - Digital Arts","post_date":"2017-02-07 12:18:04"},{"post_id":"431","post_title":"Art Director","post_date":"2017-02-07 12:18:19"}]}

下面是 PHP 脚本:

<?php
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect") ;
if(! $conn )
{
 echo 'Could not connect: ' . mysqli_error();
}
error_reporting(-1);
ini_set('display_errors', 'On');
mysqli_set_charset($conn, 'utf8');
$search ="";
if(isset($_REQUEST['query'] )){
   $search = $_REQUEST['query'];
}
if($search != ""){
   $sql = "SELECT ID,post_title,post_date FROM `wp_posts` WHERE post_title LIKE '%".$search."%'";
    mysqli_select_db($conn,'');
$query = mysqli_query($conn, $sql ) or die ("Error: ".mysqli_error($conn));;
$result = array();
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($result,
    array('post_id'=>$row['ID'],
        'post_title'=>$row['post_title'],
        'post_date'=>$row['post_date']
    ));}
    echo json_encode(array("result"=>$result));
}else{
   echo 'No search field has been sent';
}
?>

尝试以下代码:

try {
        Log.d("Jou", "result");
        JSONObject object = new JSONObject(data)
        JSONArray ja = object.getJSONArray("result");
        JSONObject jo = null;
        titles.clear();
        skills.clear();
        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            String id = jo.getString("post_id");
            String title = jo.getString("post_title");
            //String content = jo.getString("post_content");
            String date = jo.getString("post_date");
            Skill skill = new Skill();
            skill.setId(id);
            skill.setTitle(title);
            //skill.setContent(content);
            skill.setDate(date);
            skills.add(skill);
            titles.add(title);
        }
        return 1;
    } catch (JSONException e) {
        Log.d("Jou", e.getMessage());
        return 0;
    }

使用以下代码进行检查。我希望它会起作用

private int parse(String data) {
    try {
        JSONObject jsonObject = new JSONObject(data); //get a response as Json Object and then get it as array by corresponding key
        JSONArray ja = jsonObject.getJSONArray("result");
        JSONObject jo = null;
        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            String id = jo.getString("ID");
            String title = jo.getString("post_title");
            //String content = jo.getString("post_content");
            String date = jo.getString("post_date");
            Skill skill = new Skill();
            skill.setId(id);
            skill.setTitle(title);
            //skill.setContent(content);
            skill.setDate(date);
            skills.add(skill);
            titles.add(title);
        }
        return 1;
    } catch (JSONException e) {
        Log.d("TAG", e.getMessage());
        return 0;
    }
}

您的 json 响应中存在一些问题,它需要额外的字符post_date请尝试在下面的链接中对其进行验证。

http://www.jsoneditoronline.org/

最新更新