简体   繁体   中英

Extract JSON table from JSON string element

I get this JSON in response from server:

{
   "tab":[
      "[[\"2018\",11,\"19\",\"16\",\"13\"],null,null,null,null,null,\"40\"]",
      "[[\"2018\",11,\"19\",\"16\",\"19\"],null,null,null,null,null,\"56\"]",
      "[[\"2018\",11,\"19\",\"16\",\"21\"],null,null,null,null,\"57\",null]"
   ]
}

I know, that I can get first element of tab table returned using $.tab[1] . The returned element is a string that holds a table - first element of this table is another table holding a date. The question is what JSON Path expression should I use in order to extract year value from the inner table or one of those numbers at the end (40, 56, 57)?

I'm not sure what you mean when you say you can get the first element with $.tab[1] . Are you using jQuery? Even so, that doesn't seem to make any sense. Regardless, you can parse those inner tables and access them normally as arrays:

 var results = { "tab":[ "[[\\"2018\\",11,\\"19\\",\\"16\\",\\"13\\"],null,null,null,null,null,\\"40\\"]", "[[\\"2018\\",11,\\"19\\",\\"16\\",\\"19\\"],null,null,null,null,null,\\"56\\"]", "[[\\"2018\\",11,\\"19\\",\\"16\\",\\"21\\"],null,null,null,null,\\"57\\",null]" ] }; // you can refactor this as a method for more convenient usage, this is just a demo var row = JSON.parse(results.tab[0]); // now you just have a multi-dimensional array, use it as normal console.log(row[0][0]); //year console.log(row[6]); //number 

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