Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t转换为JSONArray以在ListView中显示_Java_Php_Json - Fatal编程技术网

Java Can';t转换为JSONArray以在ListView中显示

Java Can';t转换为JSONArray以在ListView中显示,java,php,json,Java,Php,Json,我正在尝试将json数组从internet转换为listview。服务器中的php代码工作正常,并返回下面的json字符串。 logcat中的json结果是: 11-09 20:47:04.170: I/AllNotes >> jSon >>(429): {"notes":{"3":{"note_subject":"dshjdsfjsdfsdhf","note_id":"4","note_date":"0000-00-00"},"2":{"note_subject":"d

我正在尝试将json数组从internet转换为listview。服务器中的php代码工作正常,并返回下面的json字符串。 logcat中的json结果是:

11-09 20:47:04.170: I/AllNotes >> jSon >>(429): {"notes":{"3":{"note_subject":"dshjdsfjsdfsdhf","note_id":"4","note_date":"0000-00-00"},"2":{"note_subject":"dshjdsfjsdfsdhf","note_id":"3","note_date":"0000-00-00"},"1":{"note_subject":"dshjdsfjsdfsdhf","note_id":"2","note_date":"0000-00-00"},"0":{"note_subject":"dshjdsfjsdfsdhf","note_id":"1","note_date":"0000-00-00"},"7":{"note_subject":"dshjdsfjsdfsdhf","note_id":"8","note_date":"1391\/8\/19"},"6":{"note_subject":"dshjdsfjsdfsdhf","note_id":"7","note_date":""},"5":{"note_subject":"dshjdsfjsdfsdhf","note_id":"6","note_date":""},"4":{"note_subject":"dshjdsfjsdfsdhf","note_id":"5","note_date":""}},
"success":"1"}
我的JSONParser类是:

package ir.mohammadi.android.nightly;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = null;

    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Make HTTP connection
        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

            Log.i("Input stream >> ", is.toString());

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            Log.i("JSON string builder >> ", json.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            jObj = new JSONObject(json.substring(1, json.length()));
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jObj;
    }
}
php代码是:

public function getNotesList($user_id)
    {
        $result = mysql_query("SELECT `id`, `note_subject`, `note_date` FROM `tbl_notes` WHERE `user_id` = '$user_id'");

        if (mysql_num_rows($result) > 0) {

            $response = array();
            $response["success"] = "1";

            $response["notes"] = array();

            while ($row = mysql_fetch_array($result)) {
                $note = array();
                $note["note_id"] = $row["id"];
                $note["note_subject"] = $row["note_subject"];
                $note["note_date"] = $row["note_date"];

                array_push($response["notes"], $note);
            }
            return $response;
        }
    }

我怎样才能解决这个问题?谢谢。

使用

  JSONObject notes = jSon.getJSONObject("notes");
而不是

JSONArray notes = jSon.getJSONArray("notes");`
因为json字符串是JSONObject的集合,所以它不包含任何JsonArray

在解析之前,您可以使用以下json检查器站点,以了解json字符串的结构

使用

  JSONObject notes = jSon.getJSONObject("notes");
而不是

JSONArray notes = jSon.getJSONArray("notes");`
因为json字符串是JSONObject的集合,所以它不包含任何JsonArray

在解析之前,您可以使用以下json检查器站点,以了解json字符串的结构


发布完整代码,因为您的json字符串只包含json对象,而不包含任何jsonArray。请将问题编辑为完整代码问题在于来自服务器的json不是json数组,而是对象的集合,每个对象都由索引键控。如果源数据中不存在JSON数组(即对象列表位于
[]
之间,没有键),Android解析器将无法创建JSON数组。我正在删除Android标记,因为这是一个与PHP服务器如何生成JSON数据直接相关的问题。为什么要剪切1个字符?jObj=newJSONObject(json.substring(1,json.length());因为在json之前显示“a”。我搜索它,发现它是utf-8 encode.post完整代码,因为您的json字符串只包含json对象,而不包含任何jsonArray。将问题编辑为完整代码问题是来自服务器的json不是json数组,它是一个对象集合,每个对象都由索引键控。如果源数据中不存在JSON数组(即对象列表位于
[]
之间,没有键),Android解析器将无法创建JSON数组。我正在删除Android标记,因为这是一个与PHP服务器如何生成JSON数据直接相关的问题。为什么要剪切1个字符?jObj=newJSONObject(json.substring(1,json.length());因为在json之前显示“a”。我搜索它,发现它是utf-8编码的。我改为JSONArray,但没有修复。在lisnk中复制JSON并正确显示结果我使用此链接创建代码:[^]()不使用JSONArray将其更改为JSONObject并查看您的JSON字符串它只包含“{”表示它只包含JSON对象,不包含任何JSONArrayTanks。修复它。在此之后,我更改为
JSONObject c=notes.getJSONObject(Integer.toString(i));
我改为JSONArray,但没有修复。在lisnk中复制JSON并正确显示结果我使用此链接创建代码:[^]()不改为JSONArray将其改为JSONObject并查看JSON字符串,它只包含“{”意味着它只包含json对象,不包含任何JSONArrayTanks。修复它。在此之后,我改为
JSONObject c=notes.getJSONObject(Integer.toString(i));