jQuery 如何正确解码使用 Html.Raw(Json.Encode(Model)) 编​​码的 JSON 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17647527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:40:57  来源:igfitidea点击:

How to properly decode a JSON string encoded using Html.Raw(Json.Encode(Model))?

javascriptjqueryasp.net-mvcjsonrazor

提问by Animesh

I am encoding some model data into a html element like this:

我正在将一些模型数据编码到这样的 html 元素中:

@Html.Raw(Json.Encode(Model));

The json string returned looks like this:

返回的 json 字符串如下所示:

{"TestList":[{"FrequencyType":"1X","GCDs":"585.6","Identifier":"6144","SeqNo":9306,"SeqNoSpecified":true,"TSeqNo":8314,"TSeqNoSpecified":true,"TestDescr":"HBsAg"},{"FrequencyType":"1X","GCDs":"585.6","Identifier":"6124","SeqNo":9295,"SeqNoSpecified":true,"TSeqNo":8315,"TSeqNoSpecified":true,"TestDescr":"HCV Ab"},{"FrequencyType":"1X","GCDs":"585.3","Identifier":"6","SeqNo":9729,"SeqNoSpecified":true,"TSeqNo":8309,"TSeqNoSpecified":true,"TestDescr":"HD Monthly LS"}],"Frequency":[{"Key":"ANNUAL","Value":"Annually"},{"Key":"BIMONTH","Value":"Bi-Monthly"},{"Key":"BIWEEK","Value":"Bi-Weekly"},{"Key":"MON","Value":"Monthly"},{"Key":"1X","Value":"One Time"},{"Key":"QTR","Value":"Quarterly"},{"Key":"SMAN","Value":"Semi-Annual"},{"Key":"WEEK","Value":"Weekly"}]};

When I try to parse this using JSON.parse, I get an error:

当我尝试使用 解析它时JSON.parse,出现错误:

arrayTestList = [];
var jsonTestList = $('#TestList').text();
jsonTestList = JSON.stringify(jsonTestList);
arrayTestList = JSON.parse(jsonTestList);
alert(arrayTestList.TestList[0]); // <===== this line is failing

Unable to get value of the property '0': object is null or undefined

How do I convert this jsonTestListstring into a javascript array so that I can access elements of arrayTestListproperly?

如何将此jsonTestList字符串转换为 javascript 数组,以便可以arrayTestList正确访问 的元素?

Edit:

编辑:

Sorry, I forgot to mention my edit. Basically above javascript code is inside a Partial View 2. The code where I am json encoding the model is in another Partial View 1. From P V 2, I cannot access the model object of P V 1, so I am just dumping the contents into a divtag, so that I can access this list TestListelement.

抱歉,我忘了提及我的编辑。基本上上面的 javascript 代码在 Partial View 2 中。我对模型进行 json 编码的代码在另一个 Partial View 1 中。从 PV 2,我无法访问 PV 1 的模型对象,所以我只是将内容转储到div标签,以便我可以访问此列表TestList元素。

采纳答案by Animesh

The issue is now resolved.

问题现已解决。

I was getting an invalid character, but couldn't immediately recognize which character it was that was causing the problem. I found that my JSON string isn't valid because of the trailing semicolon that was output by the Json.Encodemethod. I validated the JSON string @ http://jsonlint.com.

我得到了一个无效字符,但无法立即识别是哪个字符导致了问题。我发现我的 JSON 字符串无效,因为该Json.Encode方法输出的尾随分号。我验证了 JSON 字符串 @ http://jsonlint.com

Once I removed that semicolon, the json string is populated as a JavaScript array into arrayTestListobject.

一旦我删除了那个分号,json 字符串就会作为 JavaScript 数组填充到arrayTestList对象中。

Now just this works, as mentioned in both the answers above, JSON.stringifyis not needed.

现在,正如上面两个答案中提到的那样,JSON.stringify不需要这样做。

var arrayTestList = [];
var jsonTestList = $('#TestList').text().replace(";","");
arrayTestList = JSON.parse(jsonTestList);
alert(arrayTestList.TestList[0]); 

回答by karaxuna

Try removing this line:

尝试删除此行:

jsonTestList = JSON.stringify(jsonTestList);

jsonTestListis already a JSON string

jsonTestList已经是一个 JSON 字符串

回答by Satpal

Why are you using Json.Encode? Also in your code, why are you writing redundant code first you are using JSON.stringifyand the JSON.parsesame object.

你为什么使用Json.Encode?同样在您的代码中,为什么要首先编写您正在使用的冗余代码JSON.stringifyJSON.parse相同的对象。

jsonTestList = JSON.stringify(jsonTestList);
arrayTestList = JSON.parse(jsonTestList);

As per my understanding just Html.Rawwill work

根据我的理解,它Html.Raw会起作用

In JavaScript

在 JavaScript 中

var jsonObject = @Html.Raw(Model.TestList); //Here you will get JavaScript Object
var jsonTestList =  jsonObject.TestList;