如何从 JSON 对象访问一个名称值对
假设我
JavascriptSerializer oSer = new JavascriptSerializer();
string sJson = oSer.Serialize(myObject);
通过 ajax 调用返回到客户端的 json
"{\"IsValid\":false,\"EmployeeId\":null,\"fullName\":\"a\",\"EmailAddress\":\"n/a\",\"PhoneNumber\":\"n/a\"}"
从服务器传回了这个 JSON 对象,因此在 $.parseJSON(result);
之后
是否可以仅检索 IsValid value 而不循环遍历整个对象名称/值对?
更新: 似乎当 json 到达客户端时,名称值对之间的 : 更改为 = 。所以现在我必须弄清楚如何用 : 替换 = ,这样我就可以像真正的对象属性表示法一样解析和访问它。
success: function (data)
{
data.replace("=", ":");
}
不起作用。
我还将 ajax dataType 属性设置为“json”
let's say i have this JSON object passed back from the server via
JavascriptSerializer oSer = new JavascriptSerializer();
string sJson = oSer.Serialize(myObject);
the json that i get returned to my client via ajax call is
"{\"IsValid\":false,\"EmployeeId\":null,\"fullName\":\"a\",\"EmailAddress\":\"n/a\",\"PhoneNumber\":\"n/a\"}"
so after $.parseJSON(result);
is it possible to retrieve just the IsValid value without looping through the whole object name/value pairs?
UPDATE:
seems like when the json gets to the client the : gets changed into = between the name value pairs. so now i have to figure out how to replace the = with a : so i can parse and access it like a true object property notation.
success: function (data)
{
data.replace("=", ":");
}
doesn't work.
also i have the ajax dataType property set to 'json'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论如何,您不必循环遍历每个字段 - 只需将其作为
parseJSON
结果的直接属性进行访问即可。You don't have to loop through each field anyway - just access it as a direct property of the result from
parseJSON
.确保你的结果用引号引起来,单引号就可以了。
Make sure that your result is surrounded by quotation marks, single quotes are Ok.
当然:
Sure:
我发现了问题。在
我有 dataType: 'json'
这就是从服务器转换我正确配置的 JSON 的原因
i found the problem. in the
I had dataType: 'json'
that was what was converting my correctly configured JSON from the serve