0

i have a JSON code like this

    [{"url":"http:\/\/mysite.xx\/img\/exart.JPG"},{"url":"http:\/\/mysite.xx\/img\/bost.png"},
    {"url":"http:\/\/mysite.xx\/img\/03.png"},
    {"url":"http:\/\/mysite.xx\/img\/04.png"},{"url":"http:\/\/mysite.xx\/img\/05.png"},
    {"url":"http:\/\/mysite.xx\/img\/06.png"},{"url":"http:\/\/mysite.xx\/img\/07.png"},
    {"url":"http:\/\/mysite.xx\/img\/08.png"},{"url":"http:\/\/mysite.xx\/img\/09.png"},
    {"url":"http:\/\/mysite.xx\/img\/10.png"},{"url":"http:\/\/mysite.xx\/img\/11.png"},
    {"url":"http:\/\/mysite.xx\/img\/12.png"},{"url":"http:\/\/mysite.xx\/img\/13.png"},
    {"url":"http:\/\/mysite.xx\/img\/14.png"},{"url":"http:\/\/mysite.xx\/img\/15.png"},
    {"url":"http:\/\/mysite.xx\/img\/16.png"},{"url":"http:\/\/mysite.xx\/img\/17.png"},
    {"url":"http:\/\/mysite.xx\/img\/18.png"}]

how i can browse all the lines ? per example when i go to http://mysite.xx/my_post/#1 i have to see the first url in my JSON code " http://mysite.xx/img/exart.JPG " in a

Awssam Saidi
  • 435
  • 4
  • 14
  • sidenote: `#` hashtags cannot be read server side – Kevin Sep 27 '14 at 16:18
  • i wanted to browse it with javascript ! – Awssam Saidi Sep 27 '14 at 16:51
  • thats why the answer below doesn't really answer this question, i don't know why the others even upvoted the answer, when you click that link with the hashtag `#1`, that wont even reach php, making that json decode senseless, unless make an ajax call, or just plainly use javascript alone – Kevin Sep 27 '14 at 16:54
  • I think you might need to clarify your question a little bit. I'm assuming the array above is what ends up on the server side? If so, why do you want to browse it with javascript? I know almost nothing about Wordpress but I'm pretty sure it doesn't use Nodejs on the server. Or are you trying to use javascript to look at the array before it's posted? – HeadCode Sep 27 '14 at 16:56

2 Answers2

4

just use

$data = json_decode($data);
$url_5 = $data[5]['url']; // the value here is "http://mysite.xx/img/06.png"

it will give you array with hashes with 'url' key. and do whatever you want with it

Dima
  • 8,586
  • 4
  • 28
  • 57
0

You might want to try this. https://stackoverflow.com/a/1637373/1042903

Also this example from w3schools.com

<script>
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>

Reference: http://www.w3schools.com/json/json_eval.asp

If you want, you can google with this keyword, "json parse for loop javascript"

Community
  • 1
  • 1
handicop
  • 892
  • 12
  • 25