Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON解析显示了一些意外的字符错误_Javascript_Json - Fatal编程技术网

Javascript JSON解析显示了一些意外的字符错误

Javascript JSON解析显示了一些意外的字符错误,javascript,json,Javascript,Json,我想用JavaScript解析下面的JSON,然后只想写一个特定的键类型。我该怎么做?简单的JSON.parse(输出)向我显示意外的字符错误 output=[{"id":"12","category":"Smartphone","sub_category":"Tablet","name":"Tab 3","current_price":"23000","sold":"9","previous_price":"25000","description":"","image":"galaxy_tab

我想用JavaScript解析下面的JSON,然后只想写一个特定的键类型。我该怎么做?简单的JSON.parse(输出)向我显示意外的字符错误

output=[{"id":"12","category":"Smartphone","sub_category":"Tablet","name":"Tab 3","current_price":"23000","sold":"9","previous_price":"25000","description":"","image":"galaxy_tab2.jpg"},{"id":"28","category":"Accessories","sub_category":"Ebook","name":"Ebook Acc 3","current_price":"5300","sold":"8","previous_price":null,"description":"","image":"sony.jpg"},{"id":"27","category":"Accessories","sub_category":"ipad","name":"ipad Acc 4","current_price":"1200","sold":"6","previous_price":null,"description":"","image":"scott_ipad.jpg"},{"id":"3","category":"Computer","sub_category":"Laptop","name":"Laptop 3","current_price":"999","sold":"5","previous_price":"1400","description":"","image":"macbook.jpg"},{"id":"20","category":"Smartphone","sub_category":"ipad","name":"ipad 4","current_price":"29000","sold":"5","previous_price":null,"description":"","image":"ipad3.jpg"},{"id":"6","category":"Computer","sub_category":"Laptop","name":"Laptop 6","current_price":"80000","sold":"4","previous_price":"70000","description":"","image":"dell-latitude-e6430s.jpg"},{"id":"8","category":"Computer","sub_category":"Desktop","name":"Desktop 2","current_price":"28000","sold":"4","previous_price":null,"description":"","image":"compaq_pc.jpg"},{"id":"13","category":"Computer","sub_category":"Desktop","name":"Desktop 3","current_price":"29000","sold":"3","previous_price":null,"description":"","image":"gd_d4300.jpg"},{"id":"23","category":"Accessories","sub_category":"PDA","name":"PDA Acc","current_price":"2500","sold":"2","previous_price":"3000","description":"","image":"pda.jpg"},{"id":"24","category":"Accessories","sub_category":"ipad","name":"ipad Acc 3","current_price":"5600","sold":"2","previous_price":null,"description":"","image":"pen_ipad.jpg"},{"id":"1","category":"Computer","sub_category":"Laptop","name":"Laptop 1","current_price":"1199","sold":"0","previous_price":"1400","description":"","image":"macbookpro.jpg"},{"id":"2","category":"Computer","sub_category":"Laptop","name":"Laptop 2","current_price":"1499","sold":"0","previous_price":null,"description":"","image":"macbookair.jpg"},{"id":"4","category":"Computer","sub_category":"Laptop","name":"Laptop 4","current_price":"1000","sold":"0","previous_price":null,"description":"","image":"dell_vostro.jpg"},{"id":"5","category":"Computer","sub_category":"Laptop","name":"Laptop 5","current_price":"40000","sold":"0","previous_price":null,"description":"","image":"vostro_2420.jpg"},{"id":"7","category":"Computer","sub_category":"Desktop","name":"Desktop 1","current_price":"34000","sold":"0","previous_price":null,"description":"","image":"Acer_AX1400.jpg"},{"id":"9","category":"Accessories","sub_category":"Ebook","name":"Ebook Acc 1","current_price":"4000","sold":"0","previous_price":null,"description":"","image":"dragon.jpg"},{"id":"10","category":"Smartphone","sub_category":"Tablet","name":"Tab 1","current_price":"13000","sold":"0","previous_price":null,"description":"","image":"galaxy_tab.jpg"},{"id":"11","category":"Smartphone","sub_category":"Tablet","name":"Tab 2","current_price":"18000","sold":"0","previous_price":null,"description":"","image":"galaxy_tab_2-10.1.jpg"},{"id":"14","category":"Smartphone","sub_category":"ipad","name":"ipad 1","current_price":"20000","sold":"0","previous_price":null,"description":"","image":"hero.jpg"},{"id":"15","category":"Computer","sub_category":"Desktop","name":"Desktop 4","current_price":"25000","sold":"0","previous_price":null,"description":"","image":"hp_desktop.jpg"},{"id":"16","category":"Accessories","sub_category":"Ebook","name":"Ebook Acc 2","current_price":"3000","sold":"0","previous_price":null,"description":"","image":"IES1.jpg"},{"id":"17","category":"Accessories","sub_category":"ipad","name":"ipad Acc 2","current_price":"5000","sold":"0","previous_price":null,"description":"","image":"iluv-ipad.jpg"},{"id":"18","category":"Smartphone","sub_category":"ipad","name":"ipad 2","current_price":"30000","sold":"0","previous_price":null,"description":"","image":"ipad_mini.jpg"},{"id":"19","category":"Smartphone","sub_category":"ipad","name":"ipad 3","current_price":"25000","sold":"0","previous_price":null,"description":"","image":"ipad2.jpg"},{"id":"21","category":"Computer","sub_category":"Desktop","name":"Desktop 5","current_price":"36000","sold":"0","previous_price":null,"description":"","image":"lenovo_k210.jpg"},{"id":"22","category":"Computer","sub_category":"Desktop","name":"Desktop 6","current_price":"28500","sold":"0","previous_price":null,"description":"","image":"Lm_i501s.jpg"},{"id":"25","category":"Smartphone","sub_category":"Tablet","name":"Tab 4","current_price":"21000","sold":"0","previous_price":null,"description":"","image":"samsung_tab.jpg"},{"id":"26","category":"Smartphone","sub_category":"Tablet","name":"Tab 5","current_price":"23900","sold":"0","previous_price":null,"description":"","image":"samsung_tablet.jpg"},{"id":"29","category":"Accessories","sub_category":"Tablet","name":"Tab Acc 1","current_price":"4300","sold":"0","previous_price":null,"description":"","image":"tablet_kit.jpg"},{"id":"30","category":"Accessories","sub_category":"","name":"Tab Acc 2","current_price":"3600","sold":"0","previous_price":null,"description":"","image":"toshiba_tab.jpg"},{"id":"31","category":"Computer","sub_category":"Laptop","name":"Laptop 6","current_price":"42000","sold":"0","previous_price":null,"description":"","image":"vostro_2420.jpg"}]

无需对
输出中的数组调用
JSON.parse()
,因为它不是字符串。它只是一个对象数组,您可以循环访问所需的密钥

// This is *already* a valid JavaScript array of objects. There's no need to treat it like a JSON string.
var output = [{"id":"12","category":"Smartphone","sub_category":"Tablet","name":"Tab 3","current_price":"23000","sold":"9","previous_price":"25000","description":"","image":"galaxy_tab2.jpg"},{"id":"31","category":"Computer","sub_category":"Laptop","name":"Laptop 6","current_price":"42000","sold":"0","previous_price":null,"description":"","image":"vostro_2420.jpg"}]

// Just loop over the array and access the object property you want
// For example:
for (var i = 0; i < output.length; i++) {
  // Log the category of each element...
  console.log(output[i].category);
  // Or... output[i].description, output[i].image, etc...;
}

// Outputs something like:
Smartphone 
Accessories
Computer
Smartphone
Computer
Accessories
Computer
Accessories
Smartphone
Computer
Accessories
Smartphone
Computer
Smartphone
Accessories
Computer
//这是*已经*一个有效的JavaScript对象数组。没有必要将其视为JSON字符串。
var输出=[{“id”:“12”,“类别”:“智能手机”,“子_类别”:“平板电脑”,“名称”:“表3”,“当前_价格”:“23000”,“售出”:“9”,“先前_价格”:“25000”,“说明”:“图像”:“galaxy_tab2.jpg”},{“id”:“31”,“类别”:“计算机”,“子_类别”:“笔记本电脑”,“名称”:“笔记本电脑6”,“当前_价格”:“42000”,“售出”:“0”,“先前_价格”:null,“说明”:“图像”:“vostro_2420.jpg”}]
//只需在数组上循环并访问所需的对象属性
//例如:
对于(变量i=0;i
没有JSON.JavaScript对象!=JSON。:o我怎样才能将其转换为JSON,或者怎样才能从中获得预期的结果?你能帮我吗?你已经有了一个JavaScript对象,为什么需要将其转换为JSON?
JSON.parse(JSON.stringify(output))
?这样做没有意义。