How to create a javascript array in json format

Asked

Viewed 650 times

0

Example of how to declare in php:

Array
(
    [0] => http://i.vimeocdn.com/video/637696211_200x150.jpg
    [1] => http://i.vimeocdn.com/video/637696227_200x150.jpg
    [2] => http://i.vimeocdn.com/video/637696217_200x150.jpg
    [3] => http://i.vimeocdn.com/video/637696211_200x150.jpg
)
  • In JSON this would be a string with this format: [
 "http://i.vimeocdn.com/video/637696211_200x150.jpg", 
 "http://i.vimeocdn.com/video/637696227_200x150.jpg", 
 "http://i.vimeocdn.com/video/637696217_200x150.jpg",
 "http://i.vimeocdn.com/video/637696211_200x150.jpg"
 ]

  • The question was just that? or are you looking for something else with this array?

  • I wanted to build a json array with explicit index, like the example

  • You want to build a array with explicit indexes, but informs that a response that builds an object without explicit indexes was what it wanted? That did not make much sense, so I voted to close the question as insufficiently clear.

2 answers

2

Example to declare:

var myJson = {
'img' : [
 'http://i.vimeocdn.com/video/637696211_200x150.jpg',
 'http://i.vimeocdn.com/video/637696227_200x150.jpg',
 'http://i.vimeocdn.com/video/637696227_200x150.jpg',
 'http://i.vimeocdn.com/video/637696227_200x150.jpg'
]
}

Example to access:

myJson.img[0]

https://www.w3schools.com/js/js_json_arrays.asp

  • 1

    I got it, that’s it, man, that’s good..

  • 1

    I think you can choose the best answer and close question. Maybe edit the question because some people did not understand.

1

You could use the json_encode function in this array

Example:

<?php
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

    echo json_encode($arr);
?>

The above example will print:

{"a":1,"b":2,"c":3,"d":4,"e":5}

Browser other questions tagged

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