0

I'm a react-native noob. I need to read a link inside a JSON field called 'wp:featuredmedia.href'. obviusly i can't put the ':' char in the code.

I've tried in this way... but I've no success :(

componentDidMount(){
    const { navigation } = this.props;
    const id = navigation.getParam('id', );
    //const percorso = 'responseJson.wp:featuredmedia.rendered';
    return fetch('https://www.seisnet.it/wp-json/wp/v2/posts/'+id)
        .then((response) => response.json())
        .then((responseJson) => {
            this.setState({
                isLoading: false,
                title: String(responseJson.title.rendered),
                photos: String(responseJson.wp:featuredmedia),
            }, function(){});
        })
        .catch((error) =>{
            console.error(error);
        });
    }

EDIT 1 this is a section of the json file:

// 20190726085445
// https://www.seisnet.it/wp-json/wp/v2/posts/1967
  "_links": {
    "self": [
      {
        "href": "https://www.seisnet.it/wp-json/wp/v2/posts/1967"
      }
    ],
    "collection": [
      {
        "href": "https://www.seisnet.it/wp-json/wp/v2/posts"
      }
    ],
    "about": [
      {
        "href": "https://www.seisnet.it/wp-json/wp/v2/types/post"
      }
    ],
    "wp:featuredmedia": [
      {
        "embeddable": true,
        "href": "https://www.seisnet.it/wp-json/wp/v2/media/1971"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://www.seisnet.it/wp-json/wp/v2/media?parent=1967"
      }
    ],

  }
}

the field i've to read contains a link to another json file.

i've tried: JSONResponse_embedded["wp:featuredmedia"] and JSONResponse["wp:featuredmedia"]. the first give me the error "undefined is not an object" while the second give me nothing in output

Andrea Favero
  • 182
  • 1
  • 15
  • Could you please post sample json response and also expected output? – ajaykumar mp Jul 25 '19 at 16:07
  • `responseJson._embedded["wp:featuredmedia"]` as seen on this question: https://wordpress.stackexchange.com/questions/241271/wp-rest-api-details-of-latest-post-including-featured-media-url-in-one-request – GrafiCode Jul 25 '19 at 16:10

1 Answers1

3

Instead of responseJson.wp:featuredmedia, try responseJson["wp:featuredmedia"]

JavaScript object: access variable property by name as string

RSchneyer
  • 300
  • 1
  • 8