简体   繁体   中英

Convert string to array multidimensional in JavaScript

I work with socket (server and client), and trying to send a matrix; so I want to send a string array and then convert it to a variable "ARRAY" so for example, if I want to send an array with this structure.

http://i.stack.imgur.com/2hr3V.jpg

edit `

var myString = "[\"Item\", \"Count\"],[\"iPad\",2],[\"Android\",1]";

var arr = JSON.parse("[" + myString + "]");
alert(arr[0][0]);

`

i find this example, but no is a multidimensional array, i want call for example School.Section(1).User(1).Name

Make your structure in JavaScript and try JSON.stringify + JSON.parse .

Example:

var School = {
  Section: [
    {
      User: [
        {
          Name: "John Doe"
        },
        {
          Name: "Jane Doe"
        }
      ]
    }
  ]
};

var str = JSON.stringify(School);
// This gives `{"Section":[{"User":[{"Name":"John Doe"},{"Name":"Jane Doe"}]}]}`

var _school = JSON.parse(str);
// This works great!

var name = _school.Section[0].User[1].Name;
// This gives `Jane Doe`.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM