Php 如何显示响应json
我需要做出回应。例如标题Php 如何显示响应json,php,json,react-native,Php,Json,React Native,我需要做出回应。例如标题 stdClass Object ( [product] => stdClass Object ( [title] => shoes [id] => 44 [created_at] => 2018-11-08T10:58:58Z [updated_at] => 2018-11-08T10:59:01Z [type] => variable
stdClass Object
(
[product] => stdClass Object
(
[title] => shoes
[id] => 44
[created_at] => 2018-11-08T10:58:58Z
[updated_at] => 2018-11-08T10:59:01Z
[type] => variable
[status] => publish
[downloadable] =>
[virtual] =>
[permalink] => http://xxxxxxxxx/xxxxx/xxxxx/xxxxxx/
[sku] =>
[price] => 7000
[regular_price] => 0
[sale_price] =>
[price_html] =>
这是我在react native中显示响应的代码:
handlePress =()=> {
fetch('http://xxxxxxxxx/xxxx/xxxx/xxxx/xxxxx.php',{
method:'POST',
headers:{
'Content-type':'application/json'
},
body: JSON.stringify({
"type": "select",
"args": {
"table": "product",
"columns": [
"title"
],
"limit": "1"
}
})
}).then((response) => response.json())
.then((responseJson) => {
Alert.alert("Product Name " + responseJson[0].name);
}).catch((error) => {
console.error(error);
});
}
错误:Json解析错误:意外标识符“stdClass”
我认为列或表select有问题,但控制台中的错误是关于stdClass的。stdClass是PHP的一部分。您似乎做了一些类似于返回这个stdClass对象的事情(比如vardump($yourObject);) 您需要发送一个完全有效的json。在PHP中,您可以使用
echo-json\u-encode($yourObj)进行编码代码>要使用json(例如在JS中),您需要解码/解析json。在此之后,您可以像访问一般JS对象一样访问它
var json = '{"name":'BimBom', "age":42}';
obj = JSON.parse(json);
console.log(obj.name); // result in BimBom
console.log(obj.age); // result in 42
您发送的不是JSON,请参见。旁注(这不是问题,问题如上所述:您没有发送JSON):在调用response.JSON()
之前,您没有在响应上检查ok
。不仅仅是你,大多数人似乎都错过了这一点,我也错过了很多。谢谢!现在响应未定义。{“产品”:{“标题”:“\u06a9\u062a\u0628\u0644\u0646\u062f”,“id”:44,“我如何选择产品和标题?@Alireza这对你有帮助吗?