[英]how to parse Json array in Javascript
I want to parse the json array given below.this is the json i get when inspect.我想解析下面给出的 json 数组。这是我在检查时得到的 json。
0: "Harpic Power Plus Original Liquid Toilet Cleaner" 1: "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"
but when i use json.parse() i get this error但是当我使用 json.parse() 我得到这个错误
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at app.component.ts:42
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:41938)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at zone-evergreen.js:857
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41916)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at resolvePromise (zone-evergreen.js:798)
at zone-evergreen.js:864
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41916)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at drainMicroTaskQueue (zone-evergreen.js:569)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:484)
at invokeTask (zone-evergreen.js:1621)
at XMLHttpRequest.globalZoneAwareCallback (zone-evergreen.js:1658)
Hold on, so initially you've done JSON.parse('{ "product_name": [ "Harpic Power Plus Original Liquid Toilet Cleaner", "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"]}');
等等,所以最初你已经完成了
JSON.parse('{ "product_name": [ "Harpic Power Plus Original Liquid Toilet Cleaner", "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"]}');
and got an array?并得到一个数组? That's a JS array, not a JSON array.
那是一个 JS 数组,而不是 JSON 数组。 You can access its values using the array keys, eg
const foo = JSON.parse('{ "product_name": [ "Harpic Power Plus Original Liquid Toilet Cleaner", "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"]}'); console.log(foo.product_name[0]); console.log(foo.product_name[1]);
您可以使用数组键访问其值,例如
const foo = JSON.parse('{ "product_name": [ "Harpic Power Plus Original Liquid Toilet Cleaner", "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"]}'); console.log(foo.product_name[0]); console.log(foo.product_name[1]);
const foo = JSON.parse('{ "product_name": [ "Harpic Power Plus Original Liquid Toilet Cleaner", "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner"]}'); console.log(foo.product_name[0]); console.log(foo.product_name[1]);
hope this works希望这有效
a json formatted of your sample would be like this:您的样本格式的 json 将如下所示:
var myJson = { first: "Harpic Power Plus Original Liquid Toilet Cleaner", second: "HARPIC Powerplus Disinfectant Toilet Cleaner, Original - 1L Liquid Toilet Cleaner" }; console.log(JSON.stringify(myJson));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.