A
A
Alexander Ivanov2018-02-10 17:40:56
JavaScript
Alexander Ivanov, 2018-02-10 17:40:56

How to extract desired data from JSON objects?

json

{
  "response": [
    117832,
    {
      "id": 275117,
      "attachment": {
        "type": "photo",
        "photo": {
          "pid": 456257316,
          "user_id": 100,
          "src": "htkkk03379/26414/eYtW5P_DUGI.jpg",
          "src_big": "httk379/26415/G8nUUMTT2b4.jpg",
          "src_small": "httk03/v635103379/26413/KR_d0PkrQos.jpg",
          "width": 413,
          "height": 413
        }
      }
    }
  ]
}

react code
const listImages = this.props.articles.map((value, index)=>{
            if(index !== 0){                
console.log(value.attachment.photo.src); // явно не работает
                return <div key={value.id} className="posts__block">
                    <a href="#" className="posts__block__image" onClick={this.togglePopup.bind(this,index)}>
                        {value.id} <br />
                        {/*{value.text}*/}
                        {value.likes.count} <br />
                        {value.comments.count} <br />
                        {value.reposts.count} <br />
                        {/*{value.attachments.type}*/}
                    </a>
                </div>;
            }
        });

How to refer to an image in an attachment or attachments ?
In general, if you throw off at least a useful article after which I will understand how to access such deeply buried data, then I will also mark it as a decision. did not find material with examples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-02-10
@cimonlebedev

let { attachment } = data.response[1];

attachment = attachment[attachment.type];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question