繁体   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