gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:15:09 24 4
gpt4 key购买 nike

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

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

返回的 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"}]};

当我尝试使用 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

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

编辑:

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

最佳答案

尝试删除这一行:

jsonTestList = JSON.stringify(jsonTestList);

jsonTestList 已经是一个JSON字符串

关于javascript - 如何正确解码使用 Html.Raw(Json.Encode(Model)) 编​​码的 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com