在黑莓中解析JSON对象时出错

时间:2013-03-01 11:50:17

标签: java php json blackberry

我的php如下:

<?php
$str = '{
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
 },
   "Apps": [ {
"Thread1": 1,
"Thread2": 1   
"Thread3": 1
"Thread4": 1
"Thread5": 1
"Thread6": 1
"Thread7": 1
} ]
}';
echo $str;
?>

当我尝试获取“Ap​​ps”数组时,我得到以下异常:

Exception: Expected a ',' or '}' at character 165 of {
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
},
"Apps": [ {
 "Thread1": 1,
 "Thread2": 1   
"Thread3": 1
"Thread4": 1
"Thread5": 1
"Thread6": 1
"Thread7": 1
} ]
}

任何人都可以帮助/指出我在指定JSONArray时出错吗?感谢。

5 个答案:

答案 0 :(得分:0)

&#34; ThreadX&#34;:1?

之后没有逗号

检查一下:http://jsonlint.com/

答案 1 :(得分:0)

你似乎忘记了THREAD2 ... THREAD6陈述背后的逗号,

只需使用http://jsonlint.com/进行验证。

答案 2 :(得分:0)

尝试:在线程2之后忘记, ......等等。

<?php
$str = '{
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
 },
   "Apps": [ {
"Thread1": 1,
"Thread2": 1,   
"Thread3": 1,
"Thread4": 1,
"Thread5": 1,
"Thread6": 1,
"Thread7": 1
} ]
}';

$arr = json_decode($str);

echo "<pre>";
print_r($arr);

?>

答案 3 :(得分:0)

您不应该使用字符串手动创建JSON。这是一个坏主意,因为您可以轻松地破坏JSON格式(如您所愿),并且您需要确保正确地转义引号。相反,您应该在PHP结构中构建数据,然后使用json_encode()来创建实际的JSON字符串:

$array = array(
    'name' => '10.000000,106.000000',
    'Status' => array(
        'code' => 200,
        'request' => 'geocode'
    ),
    'Apps' => array(
        array(
            'Thread1' => 1,
            'Thread2' => 1,
            'Thread3' => 1,
            'Thread4' => 1,
            'Thread5' => 1,
            'Thread6' => 1,
            'Thread7' => 1
        )
    )
);

echo json_encode($array);

答案 4 :(得分:0)

<?php

$str = '{
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
 },
   "Apps": [ {
"Thread1": 1,
"Thread2": 1,   
"Thread3": 1,
"Thread4": 1,
"Thread5": 1,
"Thread6": 1,
"Thread7": 1
} ]
}';
echo "<pre>";
print_r(json_decode($str,true));
echo "<pre/>"; 
?>

Out put:

Array
(
    [name] => 10.000000,106.000000
    [Status] => Array
        (
            [code] => 200
            [request] => geocode
        )

    [Apps] => Array
        (
            [0] => Array
                (
                    [Thread1] => 1
                    [Thread2] => 1
                    [Thread3] => 1
                    [Thread4] => 1
                    [Thread5] => 1
                    [Thread6] => 1
                    [Thread7] => 1
                )

        )

)