简体   繁体   中英

How to show response json

I need to show response. For example title

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] => 

This is my code in react native to show response:

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);
});
}

Error: Json parse error: Unsuspected identifier "stdClass"

I think columns or table select have problem but in console error is about stdClass.

stdClass is part of PHP. It seems you do something like returning this stdClass-Object (like vardump($yourObject);).

You need to send an fully valid json. In PHP you can do it with echo json_encode($yourObj);

To use json (eg in JS), you need to decode/parse Json. After this, you can access it like an general JS-Object.

var json = '{"name":'BimBom', "age":42}';
obj = JSON.parse(json);

console.log(obj.name);  // result in BimBom
console.log(obj.age);  // result in 42

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM