2

次の JSON を JavaScript で解析し、特定のキー タイプのみを記述したいと考えています。どうやってやるの?単純な JSON.parse(output) は、予期しない文字エラーを示しています。

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"}]
4

1 に答える 1

2

JSON.parse()にある配列は文字列outputではないため、呼び出す必要はありません。これは、必要なキーにアクセスするためにループできるオブジェクトの配列にすぎません。

// 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
于 2012-12-13T03:05:45.697 に答える