将字符串转换为JSON对象 [英] Converting a string to JSON object

查看:114
本文介绍了将字符串转换为JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何JS认为字符串是JSON?

我有一个功能,如果JSON对象传递给它的只能。如果我传递一个字符串给它,以相同的格式为JSON,它不工作。所以,我想使该函数认为传递给它的字符串是JSON。该字符串确实是JSON格式。

我也试过以下。我输入通过Ajax的字符串,以处理为参数设置为JSON,然后当我通过了结果它的工作原理,功能

所以,我推测这个问题是不是与字符串。我如何将此字符串转换为JSON?如果我通过Ajax请求相同的字符串,然后将其传递给函数的工作原理,而直接传递给它不工作。

非常感谢!

对不起球员。

的字符串如下:

  {
     数据: [
   {
  ID:ID1,
      田:
        {
          ID:NAME1,
          标签:小丑,
          单位:年
        },
         {ID:名2,标签:数量},
    ]
      行:[数据在这里....

闭括号..
 

希望它能帮助!

解决方案

  VAR OBJ = JSON.parse(串);
 

其中,字符串是JSON字符串。

How do you make JS think that a string is 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 doesnt 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.

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.

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 doesnt work.

Thanks a lot!

Sorry guys.

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..

Hope it helps!

解决方案

var obj = JSON.parse(string);

Where string is your json string.

这篇关于将字符串转换为JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆