Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I put in HTML certain information form my json

I am having problem with my data. My JSon looks like that:

[
    {
        "link": {
            "created_at": "2013-10-07T13:31:43+09:00",
            "id": 8,
            "items_count": 4,
            "key": "0iqVSnTU-BtJ1ItVKRe2VMWvRMU",
            "mode": "standard",
            "name": "sdasadads",
            "pusher_key": "1jtsrzl3n6i1DKA3tSZJM6LPnfQ",
            "readonly_key": "R_dD5oHMsruu0YzYVKEOA8hKKXA-r",
            "updated_at": "2013-10-08T14:06:07+09:00",
            "user_id": 2
        }
    },
    {
        "link": {
            "created_at": "2013-10-07T13:32:56+09:00",
            "id": 9,
            "items_count": 1,
            "key": "Mj-6Cc-_qaGlVTPgqKexzeijYNA",
            "mode": "standard",
            "name": "Untitled2",
            "pusher_key": "hGE0D8TSar_H_Gv9MWdpj26gamM",
            "readonly_key": "T53SNKPgyf7KvRUMzDQPaM99AAc-r",
            "updated_at": "2013-10-07T13:33:14+09:00",
            "user_id": 2
        }
    },
    {
        "link": {
            "created_at": "2013-10-11T11:18:06+09:00",
            "id": 10,
            "items_count": 0,
            "key": "X_ZoKxFPHtsvSU5W11gXx1653FU",
            "mode": "standard",
            "name": "Usdadasas",
            "pusher_key": "0PZ860awofRKB9XIrXba-xY6u14",
            "readonly_key": "2rzrRZAaR7UZRK3UbUId8xmFzd4-r",
            "updated_at": "2013-10-11T11:18:06+09:00",
            "user_id": 2
        }
    }
}

I am trying to print put all the names of the links like that:

$.post( "http://0.0.0.0:9292/api/links", function( data ) {
        document.getElementById("test").innerHTML = data[link][0].name;
      });

but it doesn't work. How can I grub all the names and put it in html?

like image 755
lipenco Avatar asked Dec 09 '25 23:12

lipenco


2 Answers

  • The objects are inside the array, not the other way around.
  • link is a literal property name, not a variable containing one as a string

Thus:

data[0]['link']['name']

You'll also need to make sure that the response has an application/json content type.

Grabbing all the names will require you to use a loop and change the 0 each time round it.

like image 87
Quentin Avatar answered Dec 12 '25 11:12

Quentin


First of all change last "}" to "]" (your top structure is array not object )

Then try this

$.post( "http://0.0.0.0:9292/api/links", function( data ) {
    document.getElementById("test").innerHTML = data[0].link.name;
  });
like image 40
Ilya Neborako Avatar answered Dec 12 '25 12:12

Ilya Neborako



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!