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

4
  • sidenote: # hashtags cannot be read server side Commented Sep 27, 2014 at 16:18
  • i wanted to browse it with javascript ! Commented Sep 27, 2014 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 Commented Sep 27, 2014 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? Commented Sep 27, 2014 at 16:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

$arr = json_decode($json, true); $data = json_decode($json); thank's is it possible to do the same thing with Javascript ?
JSON.parse(json_test); is what you're looking for.
as @kolja said, its pretty same in javascript...
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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.