【问题标题】:I can't read data from JSON我无法从 JSON 读取数据
【发布时间】:2013-05-16 11:06:03
【问题描述】:

我生成一个这样的 json:

["\"纬度\":\"123.0\",\"orden\":\"0\",\"经度\":\"123.0\",\"urlfoto\":\"a \",\"idruta\":\"45\"}","{\"纬度\":\"321.0\",\"orden\":\"1\",\"经度\":\ "321.0\",\"urlfoto\":\"b\",\"idruta\":\"45\"}","{\"纬度\":\"231.0\",\"orden\" :\"2\",\"longitud\":\"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 的东西似乎有问题。看起来它编码了两次,或者是临时完成的并且有错误。修复编码器,我认为你的 PHP 脚本应该可以正常工作。
  • 试试这个,你可以自己解决问题;):json.parser.online.fr

标签: java php json


【解决方案1】:

您的 JSON 表示 字符串 的数组。所有{} 都在"..." 内,并被解释为字符串的一部分。因此,您无法在不进一步解析的情况下访问'idruta' 和其他字段,因为它们都在一个字符串中。如果可以,您应该更改 JSON 代码。

您的问题是由array.put(obj.toString()); 引起的。你不应该这样做。另外我认为你应该从obj.put("idruta",Integer.toString(p.getIdruta())); 和类似的行中删除Integer.toString。见this question

【讨论】:

    【解决方案2】:

    首先,JSON 的第一行中缺少{

    试试这个:

    $data = json_decode($puntos,true);
    

    代替:

    $data = json_decode($puntos);
    

    应该可以了!

    【讨论】:

    • 你应该解释一下第二个参数是什么以及它为什么起作用。
    猜你喜欢
    • 2015-09-03
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多