Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免视图对原始JSON输出进行双重编码_Javascript_C#_Json_Asp.net Mvc_Asp.net Core - Fatal编程技术网

Javascript 如何避免视图对原始JSON输出进行双重编码

Javascript 如何避免视图对原始JSON输出进行双重编码,javascript,c#,json,asp.net-mvc,asp.net-core,Javascript,C#,Json,Asp.net Mvc,Asp.net Core,在尝试解析从控制器获取的JSON时,我遇到以下JavaScript错误: 未捕获的SyntaxError:JSON中位置1处的意外标记 商店中的JSON.parse():76 下面是序列化元素列表的代码(因此我可以将其作为字符串发送到前端,然后使用JSON.parse())对其进行解析) 现在我认为问题在于,当我在前端获取JSON并将其记录到控制台时,我看到: {
 "type": "FeatureCollection&am

在尝试解析从控制器获取的JSON时,我遇到以下JavaScript错误:

未捕获的SyntaxError:JSON中位置1处的意外标记 商店中的JSON.parse():76

下面是序列化元素列表的代码(因此我可以将其作为字符串发送到前端,然后使用
JSON.parse()
)对其进行解析)

现在我认为问题在于,当我在前端获取JSON并将其记录到控制台时,我看到:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6350367,
          33.5841643
        ]
      },
      "properties": {
        "address": "Bigstone, 48 Rue Abou Salt Andaloussi, Casablanca 20250, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.631687,
          33.584392
        ]
      },
      "properties": {
        "address": "Rue Ibnou Al Arif, Casablanca, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.636013,
          33.5838827
        ]
      },
      "properties": {
        "address": "62 Rue Abou Ishak Al Marouni, Maarif, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.635459,
          33.584367
        ]
      },
      "properties": {
        "address": "79 Rue Abou Salt Andaloussi, Casablanca 20300, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6336024,
          33.5834623
        ]
      },
      "properties": {
        "address": "33 Rue Ahmed Barakat, Casablanca 20250, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.633257,
          33.583944
        ]
      },
      "properties": {
        "address": "39 Rue Ahmed Barakat, Casablanca 20000, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.631826,
          33.5845488
        ]
      },
      "properties": {
        "address": "10 Rue Ibnou Al Arif, Casablanca 20330, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6334731,
          33.5834792
        ]
      },
      "properties": {
        "address": "Rue Ahmed Barakat, Casablanca, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6393279,
          33.5791312
        ]
      },
      "properties": {
        "address": "Rue Al Fourate, Casablanca, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.634933,
          33.58392
        ]
      },
      "properties": {
        "address": "51 Maârif Rue Abou Salt Andaloussi, Casablanca 20000, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6357673,
          33.5820976
        ]
      },
      "properties": {
        "address": "14 Rue Ibnou Nafiss, Maarif, Morocco"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.6367801,
          33.5830291
        ]
      },
      "properties": {
        "address": "146 Rue Abou Zaid Addadoussi, Casablanca 20330, Morocco"
      }
    }
  ]
}

最后对JSON进行双重转义/编码。Razor Helper
@
在大多数情况下都会尝试对输入进行编码并返回HTML实体,例如引号、符号、大小号等

您可以使用该方法指示输入已经是编码字符串

var stores = '@Html.Raw(ViewBag.nearbyStores)';
var geojson = JSON.parse(stores);
但请注意,我会小心的。您永远不知道源变量中可能存在哪些内容会导致某种注入攻击(例如,未替换的单引号)

您应该能够完全避免额外的解析:

var stores = @Html.Raw(ViewBag.nearbyStores);
但这需要理解
nearbyStores
实际上是有效的JSON

与在控制器中序列化不同,您可以使用


如果删除
、Formatting.Indented
,结果会是什么?它对json进行了双重编码,例如,它可以显示为html。显示如何在视图中输出它。我打赌你正在做类似于
@ViewBag.nearbyStores
的事情,这将导致它用html编码entities@Daniel同样的result@pinkfloydx33像这样
var stores='@ViewBag.nearbyStores';var geojson=JSON.parse(存储)
或者更好,您可以只使用
@Html.Raw(ViewBag.nearbyStores)
,无需进一步解析。
var stores = @Html.Raw(ViewBag.nearbyStores);
//controller 
ViewBag.nearbyStores = mapFeatureCollection;
// view
var stores = @Html.Raw(Json.Serialize(ViewBag.nearbyStores));