Reputation: 725
I want to parse array in typescript.
My code is below:
interface MyObj {
val1: string
val2: string
val3: string
}
const fs = require('fs')
const file = 'test.json'
const encode = 'utf8'
const jsonString = '[{val1 : "test1", val2 : "test2", val3 : "test3"}, { val1 : "test4", val2 : "test5", val3 : "test3"}]'
fs.writeFile(file, JSON.stringify(jsonString))
fs.readFile(file, encode, (err, data) => {
const objs = JSON.parse(data) as MyObj[]
console.log(data)
console.log(objs)
for (const obj of objs) {
console.log(obj)
console.log(obj.val1)
console.log(obj.val2)
console.log(obj.val3)
}
})
Then the result is the following:
"[{val1 : \"test1\", val2 : \"test2\", val3 : \"test3\"}, { val1 : \"test4\", val2 : \"test5\", val3 : \"test6\"}]"
[{val1 : "test1", val2 : "test2", val3 : "test3"}, { val1 : "test4", val2 : "test5", val3 : "test6"}]
[
undefined
undefined
undefined
{
undefined
undefined
undefined
v
undefined
undefined
undefined
a
undefined
undefined
undefined
l
undefined
undefined
undefined
1
undefined
undefined
undefined
.
.
.
Seems parse
is successful but maybe there are some wrong syntax after parsing but cannot find them.
My environment is :
MacOS 10.12.6
NPM 5.6.0
TypeScript 2.9.1
I appreciate any helps.
Upvotes: 1
Views: 5240
Reputation: 13079
There are several problems with your code.
'[{val1 : "test1", val2 : "test2", val3 : "test3"}, { val1 : "test4", val2 : "test5", val3 : "test3"}]'
is not valid JSON. There should be double-quotes around property names.What you should do is wrap the property names in double quotes and remove the JSON.stringify
. Althoug not entirely sure why you write it to a file just to read it back?
const jsonString = '[{"val1" : "test1", "val2" : "test2", "val3" : "test3"}, { "val1" : "test4", "val2" : "test5", "val3" : "test3"}]';
fs.writeFile(file, jsonString);
fs.readFile(file, encode, ...
Upvotes: 0
Reputation: 250326
There are several problems with your code.
Firstly the json syntax mandates you quote the properties do "val1": "test1"
not val1: "test1"
.
Secondly you should use the import fs = require('fs')
syntax for the import.
Thirdly and most importantly, you write the json, as a string to the file. The jsonString
is already a string, no need to use stringify
on it again. If you do the result of parse will be a string not an object array.
interface MyObj {
val1: string
val2: string
val3: string
}
import fs = require('fs')
const file = 'test.json'
const encode = 'utf8'
const jsonString = '[{"val1" : "test1", "val2" : "test2", "val3" : "test3"}, { "val1" : "test4", "val2" : "test5", "val3" : "test3"}]'
fs.writeFileSync(file, jsonString);
fs.readFile(file, encode, (err, data) => {
const objs = JSON.parse(data) as MyObj[]
console.log(data)
console.log(objs)
for (const obj of objs) {
console.log(obj)
console.log(obj.val1)
console.log(obj.val2)
console.log(obj.val3)
}
})
Upvotes: 1
Reputation: 713
The problem is that you don't have any double quotes (") around the keys in your objects. Therefore the JSON is not valid and the browser can not parse it successfully.
So instead of
{
val1: "test1",
val2: "test2",
val3: "test3"
}
you should have
{
"val1": "test1",
"val2": "test2",
"val3": "test3"
}
You can always check that you have valid JSON on websites like jsonlint.com.
Upvotes: 0