簡體   English   中英

我無法從JSON讀取數據

[英]I can't read data from JSON

我產生這樣的json:

[“ \\” latitud \\“:\\” 123.0 \\“,\\” orden \\“:\\” 0 \\“,\\” longitud \\“:\\” 123.0 \\“,\\” urlfoto \\“:\\” a \\“, \\“ idruta \\”:\\“ 45 \\”}“,” {\\“ latitud \\”:\\“ 321.0 \\”,\\“ orden \\”:\\“ 1 \\”,\\“縱向\\”:\\“ 321.0 \\ “,\\” urlfoto \\“:\\” b \\“,\\” idruta \\“:\\” 45 \\“}”,“ {\\” latitud \\“:\\” 231.0 \\“,\\” orden \\“:\\” 2 \\“,\\”縱向\\“:\\” 231.0 \\“,\\” urlfoto \\“:\\” c \\“,\\” idruta \\“:\\” 45 \\“}”]

我在這里搜索,並且嘗試了:

$puntos = $_POST['puntos'];
$data = json_decode($puntos,true);

foreach($data as $obj) {
        $idruta = $obj['idruta'];
        $orden = $obj['orden'];
        $urlfoto = $obj['urlfoto'];
        $longitud = $obj['longitud'];
        $latitud = $obj['latitud'];
    }

非法字符串偏移量'idruta'


foreach($data as $obj) {
         $idruta = $obj->idruta;
         $orden = $obj->orden;
         $urlfoto = $obj->urlfoto;
         $longitud = $obj->longitud;
         $latitud = $obj->latitud;
     }

試圖獲取非對象的屬性


foreach($data as $obj) {
        $idruta = $obj[0];
        $orden = $obj[1];
        $urlfoto = $obj[2];
        $longitud = $obj[3];
        $latitud = $obj[4];
    }

obj [i]始終為0,沒有錯誤。

循環執行3次,這樣就可以了。

抱歉,我只是在學習JSON和php,如果有人可以幫助我獲取JSON數據,我將非常高興。

謝謝!

編輯:感謝您的答案! 我不知道為什么會丟失“ {”,例如,當我在JSONlint中粘貼相同的json時,它可以很好地進行驗證,所以...我有點遺憾。

這就是我發送json的方式:

public void insertPoints(ArrayList<Punto> puntos){
        JSONArray array = new JSONArray();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for(Punto p:puntos){
            JSONObject obj = new JSONObject();
            try {
            obj.put("idruta",Integer.toString(p.getIdruta()));
            obj.put("orden",Integer.toString(p.getOrden()));
            obj.put("urlfoto",p.getUrlfoto());
            obj.put("longitud",Double.toString(p.getLongitud()));
            obj.put("latitud",Double.toString(p.getLongitud()));
            array.put(obj.toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        HttpClient httpClient = new DefaultHttpClient();

        try {
            HttpPost request = new HttpPost(CREATE_POINT);
            StringEntity params =new StringEntity("puntos=" + postjson);
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            // handle response here...

        } catch (Exception ex) {
            // handle exception here
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        }

這有什么問題嗎?

謝謝!

首先,JSON第一行中缺少{

嘗試這個:

$data = json_decode($puntos,true);

代替:

$data = json_decode($puntos);

它應該工作!

您的JSON表示一個字符串數組。 所有{}都在"..."內部,並被解釋為字符串的一部分。 因此,您不能在不進行進一步解析的情況下訪問'idruta'和其他字段,因為它們都在單個字符串中。 如果可以,您應該更改JSON代碼。

您的問題是由array.put(obj.toString());引起的array.put(obj.toString()); 你不應該這樣做。 我也認為您應該從obj.put("idruta",Integer.toString(p.getIdruta()));刪除Integer.toString obj.put("idruta",Integer.toString(p.getIdruta())); 和類似的線條。 看到這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM