如何从API解码JSON响应 [英] How to decode a json response from API

查看:91
本文介绍了如何从API解码JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从这样的API调用数据:

I am calling data from an API like this:

$curl = curl_init();
//adding fields to the curl object to enter the site
curl_setopt($curl, CURLOPT_URL, $my_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);  
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

//executing the curl call and getting data back
$jsonStr = curl_exec($curl);

curl_close($curl); // close the connection

print_r($jsonStr);

它工作正常,但问题是我无法解码json响应.我得到这样的东西:

it is working fine but the thing is that I cannot decode the json response. I am getting something like this:

[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]

我试图对其进行解码,并调用诸如"echo $ code [0] ['name'];"之类的值.我不能,它显示了相同的json数组,然后我尝试将json数组保存到一个变量中,例如:

I tried to decode that and calling a value like "echo $code[0]['name'];" and I couldn´t, it shows the same json array, then I tried to save the json array into a variable like:

$json = '[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]';

并尝试对其进行解码,并且可以正常工作,我认为问题出在单引号上.我不确定,但这是根据我得到的结果所假设的.我是curl,json和php的新手,所以如果有人可以帮助我.

and tried to decode it and it works, I think the problems is with single quotations.I am not sure but it is what I am assuming base on the results I am getting. I am new at curl, json and php so if someone can help me.

顺便说一句,我正在尝试这样解码:

By the way, I am trying to decode like this:

$code = json_decode($jsonStr ,true);
echo $code[0]['id'];

但是它不起作用,它返回相同的完整json数组,而不是我尝试获取的值

but it is not working, it returns the same complete json array and not the value I am trying to get

当我试图回显$ jsonStr时,我得到:

when I am trying to echoing $jsonStr I get:

[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]

推荐答案

不确定为什么对您不起作用.当代码中包含json字符串时,它确实起作用.

Not sure why is not working for you. When the json string is in the code it does work.

<?php

$json = '[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]';

$arr = json_decode($json, true);

$id = $arr[0]['id'];
$name = $arr[0]['name'];
print("$id $name\n");

输出:

1 Books

顺便说一下我的版本

PHP 5.6.30 (cli) (built: Aug  8 2017 12:20:45)

这篇关于如何从API解码JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆