How to transform a JSON that contains an object array into an object list?

Asked

Viewed 190 times

0

Good evening. I have the following JSON:

 {
  "_id": {
  "$oid": "3432fa43242"
 },
 "posts": [{
 "thread": {
    "uuid": "2911da",
    "url": "http://www.google.com",
    "site_full": "opiniaoenoticia.com.br",
    "site": "opiniaoenoticia.com.br",
    "site_section": "http://opiniaoenoticia.com.br/feed/",
    "site_categories": [],
    "section_title": "Opinião e Notícia",
    "title": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
    "title_full": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
    "published": "2016-08-08T18:07:00.000+03:00",
    "replies_count": 0,
    "participants_count": 1,
    "site_type": "news",
    "country": "BR",
    "spam_score": 0.0,
    "main_image": "",
    "performance_score": 0,
    "domain_rank": null,
    "social": {
        "facebook": {
            "likes": 0,
            "comments": 0,
            "shares": 0
        },
        "gplus": {
            "shares": 0
        },
        "pinterest": {
            "shares": 0
        },
        "linkedin": {
            "shares": 0
        },
        "stumbledupon": {
            "shares": 0
        }
    }
},
"uuid": "2931da",
"url": "http://3NXmoxXP9jWxZXABLjFrpB03",
"ord_in_thread": 0,
"author": "Melissa",
"published": "2016-08-08T18:07:00.000+03:00",
"title": "Veto a protestos contra Temer na Rio 2016 gera polêmica",
"text": "Início » Brasil » Veto a protestos contra Temer na Rio 2016 gera polêmica.",
"highlightText": "",
"highlightTitle": "",
"language": "portuguese",
"external_links": [],
"entities": {
    "persons": [],
    "organizations": [],
    "locations": []
},
"crawled": "2016-08-08T22:09:19.938+03:00"
}, {
 "thread": {...

How to turn the object array posts into an object list? I have the data saved in Mongodb.

Thank you.

  • Is your problem with JSON (working in php, javascript, .net, etc) or with the data stored in mongodb? Other, what do you mean by "object list"? The array is already a list of objects.

1 answer

0

php has a specific function for this:

json_decode(string_json,boolean)

The second parameter concerns whether in the conversion you want to return an associative array or not.

Your example would be:

$json_retornado = '...' // Entre aspas simples
$objeto_php = json_encode($json_retornado)

This way you will have a navigable php object in the form:

$objeto_php->_id->$oid

Browser other questions tagged

You are not signed in. Login or sign up in order to post.