简体   繁体   English

从JSON字符串元素中提取JSON表

[英]Extract JSON table from JSON string element

I get this JSON in response from server: 我从服务器获取此JSON作为响应:

{
   "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] . 我知道,我可以使用$.tab[1]返回tab表的第一个元素。 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)? 问题是我应该使用哪个JSON Path表达式来从内部表或末尾的那些数字之一(40、56、57)中提取年份值?

I'm not sure what you mean when you say you can get the first element with $.tab[1] . 我不确定当您说可以使用$.tab[1]获得第一个元素时是什么意思。 Are you using jQuery? 您正在使用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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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