json string:
str = "{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx',
'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx',
'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf',
'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}"
expecting:
json = {'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx',
'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx',
'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf',
'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}
i am trying with JSON.parse(str); it gives me error:
VM267:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:1:6
How can i convert string to pure json.
please have a look into this.
JSON.parse
expects a well formed string. JSON data is written as name/value pairs. A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value (in double quotes if its a string value).
In addition, it looks like you're attempting to describe a collection of objects, so you should wrap it all inside an array... (note i'm using a `` ES2015 string, that allows multiline strings)
const str = `[{
"Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx",
"Title": "Apply Online"
},
{
"Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx",
"Title": "Login"
},
{
"Link": "media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf",
"Title": "Notification "
},
{
"Link": "http://www.powergridindia.com/",
"Title": "Official Website"
}]`;
console.log(JSON.parse(str)[0].Title); // Apply Online
str = "what 'ever'";
str = str.replace(/'/gi,'"')
this is the way to replace all with regexp and replace()
Your json is invalid.
[]
which indicate array so to make it valid json, wrap objects with brackets and replace all single quote, and then parse it.
str = `[{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx', 'Title': 'Apply Online'},{'Link': 'https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx', 'Title': 'Login'},{'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf', 'Title': 'Notification '},{'Link': 'http://www.powergridindia.com/', 'Title': 'Official Website'}]` json = JSON.parse(str.replace(/'/g, '"')) console.log(json)
You are missing the outermost wrapper for whatever array you have going on here. Also double quotes are the standard in json
Try wrapping your fields.
`{
"items": [
{
"Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/default.aspx",
"Title": "Apply Online"
}, {
"Link": "https://careers.powergrid.in/CCBaclogVacancy2018/c/login.aspx",
"Title": "Login"
}, {
"Link": "media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8588011698.pdf",
"Title": "Notification"
}, {
"Link": "http://www.powergridindia.com",
"Title": "Official Website"
}
]
}`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.