Json 文件不起作用/未读取 jsonarray 无法转换为 jsonobject


Json file is not working/not reading jsonarray cannot be converted to jsonobject

您好,我想在我的安卓应用程序中访问我的网络记录。通过这样做,我使用JSON和PHP来做到这一点。这是我的json文件的网址:这里

但问题是它只是显示 php 代码。我需要能够访问/读取该文件。:(知道我在这里做什么吗?我非常感谢帮助。谢谢。

这是我到目前为止尝试过的:

    <?php 
    include('connectdb.php');
    $sql = "SELECT salesordercard_code, location_from, location_to, salesmancode FROM salesorderingcard";
    $result = mysql_query($sql);    
    if($result === FALSE) {
     die(mysql_error()); // TODO: better error handling
    }
    $set = array();
    while($row1 = mysql_fetch_assoc($result)) {
        $set[] = $row1;
    }
    header('Content-type: application/json');
    echo json_encode($set);

    ?>  

主要活动.class

@Override
protected Void doInBackground(Void... params) {
        // Create the array 
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");
        try {
            // Locate the array name
            jsonarray = jsonobject.getJSONArray("posts");
            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobject.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobject.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobject.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobject.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

日志猫:

            11-18 02:35:08.521: E/log_tag(1047): Error parsing data org.json.JSONException: Value [{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0001"},{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0002"}] of type org.json.JSONArray cannot be converted to JSONObject

首先作为 json 字符串

[ // Its an Array
    "posts",// Its not an Array and its index is 0 so we will not use index 0
    {//Index1
        "salesordercard_code": "SLESO0001",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    },
    {//index2
        "salesordercard_code": "SLESO0002",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    }
]

更改您的 doInBackground() 方法,如下所示,

@Override
protected Void doInBackground(Void... params) {
        // Create the array 
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");
        try {
            // Locate the array name
            jsonarray = new JSONArray(jsonobject.toString());
            for (int i = 1; i < jsonarray.length(); i++) {// strat from index1 
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonobj = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobj.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobj.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobj.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobj.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

希望这能解决您的问题。

您的results.json文件无法运行 php,因为它没有被处理为 PHP,而是作为 JSON 文件处理,因此它将其中的文本显示为文本。

将其重命名为 results.php 并尝试,然后,它应该打印出带有所需结果的 json 编码数据。