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

时间:2017-03-25 05:28:11

标签: android json

我收到此错误: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';
}
?>

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

请尝试以下代码:

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;
    }

答案 2 :(得分:0)

你的json响应中存在一些问题,它在post_date中使用额外的字符尝试在下面的链接中验证它。

http://www.jsoneditoronline.org/