Javascript JSON解析字符串

Javascript JSON解析字符串,javascript,json,Javascript,Json,我正在使用JSON.stringify对对象进行字符串化。结果字符串如下所示 '{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef

我正在使用
JSON.stringify
对对象进行字符串化。结果字符串如下所示

'{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"150\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"37\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"71\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"85\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"134\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}]","startIndex":"0","totalObjects":"6"}'
现在,当我尝试使用
JSON.parse()
再次将其解析为对象时。它给了我一个错误“
未捕获的语法错误:意外的令牌@
”。我看到有些键以@开头,但我无法获得错误或找到任何修复

请帮忙

{\"@type

我认为这是您的问题……不希望的转义“

您的原始JSON对象是什么,因为您的stringify返回了错误的JSON字符串

我假设您的原始json是

{
    "@type": "page",
    "count": "6",
    "endIndex": "0",
    "objects": [
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "92",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "150",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "37",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "71",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "85",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        },
        {
            "@type": "viewableObject",
            "name": "Body1",
            "objectDbId": "134",
            "objectId": "allViews",
            "resourceGuid": "c2c06248-53e2-4342-8f8a-df25ef40bd1a",
            "type": "view"
        }
    ]
}
并使用JSON.stringify返回

"{"@type":"page","count":"6","endIndex":"0","objects":[{"@type":"viewableObject","name":"Body1","objectDbId":"92","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"150","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"37","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"71","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"85","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"},{"@type":"viewableObject","name":"Body1","objectDbId":"134","objectId":"allViews","resourceGuid":"c2c06248-53e2-4342-8f8a-df25ef40bd1a","type":"view"}]}"
你能发布你的原始json吗

结果字符串如下所示

'{"@type":"page","count":"6","endIndex":"0","objects":"[{\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"92\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"150\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"37\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"71\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"85\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}, {\"@type\": \"viewableObject\", \"name\": \"Body1\", \"objectDbId\": \"134\", \"objectId\": \"allViews\", \"resourceGuid\": \"c2c06248-53e2-4342-8f8a-df25ef40bd1a\", \"type\": \"view\"}]","startIndex":"0","totalObjects":"6"}'
这似乎只是作为输出得到的,例如从
console.log
。它不是您拥有的字符串的字符串文字表示,而是
“'+jsonstring+”

如果您已经将该输出用作字符串文本,并且从中得到了解析错误,那么您最好使用

'{"@type":"page","count":"6","endIndex":"0","objects":"[{\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"92\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"150\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"37\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"71\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"85\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}, {\\"@type\\": \\"viewableObject\\", \\"name\\": \\"Body1\\", \\"objectDbId\\": \\"134\\", \\"objectId\\": \\"allViews\\", \\"resourceGuid\\": \\"c2c06248-53e2-4342-8f8a-df25ef40bd1a\\", \\"type\\": \\"view\\"}]","startIndex":"0","totalObjects":"6"}'
当然,正如@FelixKling在评论中提到的,在JSON中包含JSON字符串是一种反模式(就像在字符串文本中包含JSON一样)


请发布您如何字符串化对象以及何时/如何再次尝试解析JSON。看起来您在正确转义
时遇到了问题。更好的解决方案是不让JSON包含其他JSON(
对象
应该编码为数组,而不是包含JSON的字符串)。出于某种原因,我不得不从某个webapp获取一个对象,并在其他地方硬编码它以测试某些功能。为此,我在开发者控制台上调用JSON.stringify并使用这个字符串。这行得通吗?@KingJames:我怀疑“未完成转义”而不是“不想要的转义”,我原来的JSON和你写的是一样的。那么你用什么来字符串化它呢?因为在我这边,它给了我正确的字符串。我使用的是JSON.stringify。你是说不想要的逃跑是我得到的不正确的东西吗?显然是的。它的javascript JSON.stringify是吗?是的。它总是给我逃避角色。是因为我是在开发者控制台里做的吗?