admin管理员组文章数量:1401589
I'm messing around with building a JSON object, so I can push results from a DB query into it, Then returning the populated JSON data.. But running into a problem from the get go.
This is My current code:
//var AppenateResString = {name:"777"};
var AppenateResString = {
"DataSources": [{
"Id": "TEST",
"Rows": [
["10011", "10011 - Test Generic Project"]
],
"NewRows": [],
"DeletedRows": []
}],
"LastUpdated": ""
};
var AppenateResBody = JSON.parse(AppenateResString);
response.body = JSON.stringify(AppenateResBody);
I'm messing around with building a JSON object, so I can push results from a DB query into it, Then returning the populated JSON data.. But running into a problem from the get go.
This is My current code:
//var AppenateResString = {name:"777"};
var AppenateResString = {
"DataSources": [{
"Id": "TEST",
"Rows": [
["10011", "10011 - Test Generic Project"]
],
"NewRows": [],
"DeletedRows": []
}],
"LastUpdated": ""
};
var AppenateResBody = JSON.parse(AppenateResString);
response.body = JSON.stringify(AppenateResBody);
But this is the Error I'm getting:
JavaScript Error: SyntaxError: invalid character 'o' looking for beginning of value
at
<anonymous>:21:23
--
};
var AppenateResBody = JSON.parse(AppenateResString);
^ Error!
I've narrowed it down to something is wrong with my JSON layout.. or how I'm building the JSON object.. or some basic just, "This is how you do JSON" kindof thing. But I can't figure it out.
Any ideas?
P.S. As you can see I have mented out just what I thought would be a very simple JSON object to build.. it fails the same.
Share Improve this question asked Feb 11, 2016 at 21:52 DarbyMDarbyM 1,2032 gold badges11 silver badges25 bronze badges 3-
that's not json. that's just a regular javascript variable assignemt. there's no need to parse anything,b ecause that "json" was parsed when the entire
<script>
block got parsed by the javascript engine. if you want actual json and really want to parse it, then you need to assign the json as a string:var foo = 'json_goes_here';
– Marc B Commented Feb 11, 2016 at 21:54 -
2
Since that is just a variable assignment of actual javascript object/array data, you're probably getting a magic "stringified" version of that, which would be the text
[object Object]
, which is an invalid array definition in JSON terms, hence theunexpected o
– Marc B Commented Feb 11, 2016 at 21:56 - Possible duplicate of I keep getting "Uncaught SyntaxError: Unexpected token o" – Brian Ray Commented Feb 11, 2016 at 22:04
5 Answers
Reset to default 6AppenateResString
isn't JSON at all. It is a JavaScript object (and not a string).
JSON is a serialisation format. If you have any in JavaScript then it will be expressed as a string.
This is a javascript object
var AppenateResString = {
"DataSources": [{
"Id": "TEST",
"Rows": [
["10011", "10011 - Test Generic Project"]
],
"NewRows": [],
"DeletedRows": []
}],
"LastUpdated": ""
};
if you want it as a JSON string then
var str = JSON.stringify(AppenateResString);
if you want the JSON string back to an javascript object then
var obj = JSON.parse(str);
Your just over plicating things. You don't need to JSON.parse the object. It's already in the format you need.
<script type="text/javascript">
var AppenateResString = {
"DataSources": [{
"Id": "TEST",
"Rows": [
["10011", "10011 - Test Generic Project"]
],
"NewRows": [],
"DeletedRows": []
}],
"LastUpdated": ""
};
response.body = JSON.stringify(AppenateResString);
</script>
var AppenateResBody = AppenateResString;
Instead of
var AppenateResBody = JSON.parse(AppenateResString);
It is an object
See Below:
I keep getting "Uncaught SyntaxError: Unexpected token o"
You're attempting to call JSON.parse
on something that's already an object, and that's why you're getting the error.
The error message stating invalid character 'o' looking for beginning of value
is happening because it's literally attempting to parse the word 'object' instead of an actual data structure.
本文标签: javascriptWhat is wrong with my JSON dataStack Overflow
版权声明:本文标题:javascript - What is wrong with my JSON data? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744292462a2599182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论