Javascript 将字符串转换为 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10976897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a string to JSON object
提问by Zer0
How do you make JS think that a string is JSON ?
你如何让 JS 认为一个字符串是 JSON ?
I have a function which only works if JSON object is passed to it. If I pass a string to it, with same format as JSON, it doesn't work. So I want to make that function think that the string passed to it is a JSON. The string is indeed in the JSON format.
我有一个函数,它只在 JSON 对象传递给它时才有效。如果我向它传递一个字符串,格式与 JSON 相同,则它不起作用。所以我想让这个函数认为传递给它的字符串是一个 JSON。该字符串确实是 JSON 格式。
I also tried the following. I inputted the string through Ajax , with "handle as" parameter as "JSON", and then when I passed the result to the function it works.
我也尝试了以下方法。我通过 Ajax 输入字符串,“handle as”参数为“JSON”,然后当我将结果传递给函数时,它起作用了。
So I deduced the problem is not with the string. How do I convert this string to JSON? If i get same string through ajax request and then passing it to function works, whereas directly passing it doesn't work.
所以我推断问题不在于字符串。如何将此字符串转换为 JSON?如果我通过 ajax 请求获得相同的字符串,然后将其传递给函数工作,而直接传递它不起作用。
The string is as follows:
字符串如下:
{
"data": [
{
"id": "id1",
"fields": [
{
"id": "name1",
"label": "joker",
"unit": "year"
},
{"id": "name2", "label": "Quantity"},
],
"rows": [ data here....
and closing braces..
回答by Kshitij
var obj = JSON.parse(string);
Where string
is your json string.
string
你的json字符串在哪里。
回答by Sarfraz
You can use the JSON.parse()
for that.
您可以JSON.parse()
为此使用。
Example:
例子:
var myObj = JSON.parse('{"p": 5}');
console.log(myObj);
回答by Abraham
I had the same problem with a similar string like yours
我对像你这样的类似字符串有同样的问题
{id:1,field1:"someField"},{id:2,field1:"someOtherField"}
The problem here is the structure of the string. The json parser wasn't recognizing that it needs to create 2 objects in this case. So what I did is kind of silly, I just re-structured my string and added the []
with this the parser recognized
这里的问题是字符串的结构。在这种情况下,json 解析器没有意识到它需要创建 2 个对象。所以我所做的有点愚蠢,我只是重新构造了我的字符串并添加[]
了解析器识别的
var myString = {id:1,field1:"someField"},{id:2,field1:"someOtherField"}
myString = '[' + myString +']'
var json = $.parseJSON(myString)
Hope it helps,
希望能帮助到你,
If anyone has a more elegant approach please share.
如果有人有更优雅的方法,请分享。
回答by sandeep patel
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );
link:-
关联:-
回答by Sugan V
convert the string to HashMap using Object Mapper ...
使用 Object Mapper 将字符串转换为 HashMap ...
new ObjectMapper().readValue(string, Map.class);
new ObjectMapper().readValue(string, Map.class);
Internally Map will behave as JSON Object
内部 Map 将表现为 JSON 对象
回答by Ankita_systematix
var Data=[{"id": "name2", "label": "Quantity"}]
Pass the string variable into Json parse :
将字符串变量传递给 Json parse :
Objdata= Json.parse(Data);
回答by Siyavash Hamdi
Simply use eval
function.
简单地使用eval
函数。
var myJson = eval(theJsibStr);
回答by Shishir
Let's us consider you have string like
让我们考虑你有这样的字符串
example : "name:lucy,age:21,gender:female"
例如:“姓名:露西,年龄:21,性别:女性”
function getJsonData(query){
let arrayOfKeyValues = query.split(',');
let modifiedArray = new Array();
console.log(arrayOfKeyValues);
for(let i=0;i< arrayOfKeyValues.length;i++){
let arrayValues = arrayOfKeyValues[i].split(':');
let arrayString ='"'+arrayValues[0]+'"'+':'+'"'+arrayValues[1]+'"';
modifiedArray.push(arrayString);
}
let jsonDataString = '{'+modifiedArray.toString()+'}';
let jsonData = JSON.parse(jsonDataString);
console.log(jsonData);
console.log(typeof jsonData);
return jsonData;
}
let query = "name:lucy,age:21,gender:female";
let response = getJsonData(query);
console.log(response);
`
`
回答by Vinod Selvin
JSON.parse()
function will do.
JSON.parse()
功能会做。
or
或者
Using Jquery,
使用jQuery,
var obj = jQuery.parseJSON( '{ "name": "Vinod" }' );
alert( obj.name === "Vinod" );