admin管理员组文章数量:1304893
Need help to convert below JSON string into JSON object.Even string JSON is valid json (verified by /).
JSON:
{
"condition": "AND",
"rules": [{
"id": "amount",
"operator": "greater_or_equal",
"value": "900"
},
{
"condition": "AND",
"rules": [{
"id": "vendorname",
"operator": "equal",
"value": "US BANK NATIONAL ASSOCIATION"
},
{
"id": "vendorname",
"operator": "equal",
"value": "HANSEN SOLUTIONS LLC"
}
]
}
]
}
Need help to convert below JSON string into JSON object.Even string JSON is valid json (verified by https://jsonlint./).
JSON:
{
"condition": "AND",
"rules": [{
"id": "amount",
"operator": "greater_or_equal",
"value": "900"
},
{
"condition": "AND",
"rules": [{
"id": "vendorname",
"operator": "equal",
"value": "US BANK NATIONAL ASSOCIATION"
},
{
"id": "vendorname",
"operator": "equal",
"value": "HANSEN SOLUTIONS LLC"
}
]
}
]
}
Share
Improve this question
edited Aug 5, 2017 at 10:25
steliosbl
8,9214 gold badges32 silver badges57 bronze badges
asked Aug 5, 2017 at 7:46
Om ChaturvediOm Chaturvedi
1031 gold badge2 silver badges10 bronze badges
10
- What is the error, what doesn't work? You've only got half a question. – ste2425 Commented Aug 5, 2017 at 7:48
-
1
JSON.parse
... – Pranav C Balan Commented Aug 5, 2017 at 7:48 - Please add code runs converting. – sfrehse Commented Aug 5, 2017 at 7:48
- Error in console log --Uncaught SyntaxError: Invalid or unexpected token – Om Chaturvedi Commented Aug 5, 2017 at 7:53
- 1 @OmChaturvedi I just tried this in chrome dev tools looks fine tbh imgur./a/LslsH – adiga Commented Aug 5, 2017 at 7:59
3 Answers
Reset to default 4Your JSON string is multiline. Multiline string should be stored using template literals, otherwise use string concatenation to represent your string.
The below exmaple uses template literals. It is used to represent multi line string.
var str = `{
"condition": "AND",
"rules": [{
"id": "amount",
"operator": "greater_or_equal",
"value": "900"
},
{
"condition": "AND",
"rules": [{
"id": "vendorname",
"operator": "equal",
"value": "US BANK NATIONAL ASSOCIATION"
},
{
"id": "vendorname",
"operator": "equal",
"value": "HANSEN SOLUTIONS LLC"
}
]
}
]
}`;
console.log(JSON.parse(str));
This is a single line string.
var str = '{"condition":"AND","rules":[{"id":"amount","operator":"greater_or_equal","value":"900"},{"condition":"AND","rules":[{"id":"vendorname","operator":"equal","value":"US BANK NATIONAL ASSOCIATION"},{"id":"vendorname","operator":"equal","value":"HANSEN SOLUTIONS LLC"}]}]}';
console.log(JSON.parse(str));
Need help to convert below JSON string into JSON object.Even string JSON is valid json (verified by https://jsonlint./).
JSON.parse(jsonString); Is a pure JavaScript approach so long as you can require a reasonably modern browser.
See also https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Update:
Try JSON.parse(JSON.stringify(TheString))
Just using
try {
let obj = JSON.parse( string);
} catch( e) {
// conversion fails
console.error( e )
}
本文标签: javascriptJSONparse is not working for converting JSON string to JSON objectStack Overflow
版权声明:本文标题:javascript - JSON.parse is not working for converting JSON string to JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759783a2396333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论