我的json有问题, 我使用PHP Curl发布带有表单数据的文件,并在发布后回滚一些奇怪的json
像这样{"code":200,"status":"Success","badges":{"id":"0d28f02d13fe4c7ca8681e2ed0224180","name":"test 5","description":"test 5","image":"http://somesite.id/images/d25273bdc1b54525a602dfca8ab826fdtest.jpg","category":"Upload","target":2}}
问题是,返回的结果不能是json_decode,我知道如果这个json很奇怪,我可以打印但不能是json_decode,
我无法访问更改API,我需要来自徽章的ID, 有什么诀窍吗?或者获取id的方法,或者一些修改后的返回结果可以解码
我试过了
$a = 'that result';
$b = "'".$a."'";
$c = json_decode($b);
但它仍然是json格式,而不是成为数组 需要一些想法和帮助,thx
编辑:
让我们说
$result = {"code":200,"status":"Success","badges":{"id":"0d28f02d13fe4c7ca8681e2ed0224180","name":"test 5","description":"test 5","image":"http://somesite.id/images/d25273bdc1b54525a602dfca8ab826fdtest.jpg","category":"Upload","target":2}}
when I json_decode
$data = json_decode($result);
var_dump($result );
print_r($data);
它会返回
{"code":200,"status":"Success","badges":{"id":"0d28f02d13fe4c7ca8681e2ed0224180","name":"test 5","description":"test 5","image":"http://somesite.id/images/d25273bdc1b54525a602dfca8ab826fdtest.jpg","category":"Upload","target":2}}bool(true) 1
答案 0 :(得分:2)
试试这个
$js='{"code":200,"status":"Success","badges":{"id":"0d28f02d13fe4c7ca8681e2ed0224180","name":"test 5","description":"test 5","image":"http://somesite.id/images/d25273bdc1b54525a602dfca8ab826fdtest.jpg","category":"Upload","target":2}}';
var_dump(json_decode($js,true));
您的输出将是
array (size=3)
'code' => int 200
'status' => string 'Success' (length=7)
'badges' =>
array (size=6)
'id' => string '0d28f02d13fe4c7ca8681e2ed0224180' (length=32)
'name' => string 'test 5' (length=6)
'description' => string 'test 5' (length=6)
'image' => string 'http://somesite.id/images/d25273bdc1b54525a602dfca8ab826fdtest.jpg' (length=66)
'category' => string 'Upload' (length=6)
'target' => int 2
答案 1 :(得分:1)
您不希望在字符串周围加上引号。变化
$a = 'that result';
$b = "'".$a."'";
$c = json_decode($b);
简单地
$a = 'that result';
$c = json_decode($a);
JSON有效。将引号括起来使无效。
答案 2 :(得分:0)
原始响应是正确的JSON语法。请勿在{{1}}
中添加额外的引号而是尝试这个,作为一个例子:
$b = "'".$a."'"; //INCORRECT
哪个会打印:
/* $response is the result from API */
$response = '{"code":200,"status":"Success","badges":{"id":"0d28f02d13fe4c7ca8681e2ed0224180"}}';
$decoded = json_decode($response);
print_r($decoded); //See full decoded
$badge_id = $decoded->badges->id;
echo "The badge id is $badge_id \n";