【问题标题】:How to serialise a JSON string inside a JSON object如何在 JSON 对象中序列化 JSON 字符串
【发布时间】:2018-05-18 04:08:14
【问题描述】:

我有一个包含属性“数据”的对象。此属性包含 Json 格式的附加元数据。

但是,我现在需要将整个对象作为 Json 字符串发送到网页。我正在使用带有以下代码的 .Net Core 2.0

var content = JsonConvert.SerializeObject(model);
content = content.Replace("\\r\\n", "");
return View("Index", content);

在 StartUp.ConfigureService 我还有以下内容

services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

不幸的是,我现在得到了

未捕获的 SyntaxError:JSON 中第 3159 位的意外标记 i 在 JSON.parse() 处

如何确保我的 Data 属性在实际需要之前不会被视为 Json(在这种情况下,我将使用 JQuery 反序列化我需要的位)

正在返回的我的 Json 字符串看起来像这样

{content:"this", foo: "bar", data:"["array1","array2","array3"]" }

显然我有报价问题.....

谢谢

【问题讨论】:

  • 也许不要这样做content = content.Replace("\\r\\n", "");

标签: jquery json reactjs asp.net-core-mvc


【解决方案1】:

解决了这个问题。

我只需要转义数据属性 Json 中的双引号,以便完整的 Json 像这样返回

{content:"this", foo: "bar", data:"[\"array1\",\"array2\",\"array3\"]" }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多