Javascript MVC3 jquery ajax json数据
我试图解析从控制器调用的Web服务中检索到的json。 现在,为了显示json字符串,我已经这样做了Javascript MVC3 jquery ajax json数据,javascript,json,asp.net-mvc-3,Javascript,Json,Asp.net Mvc 3,我试图解析从控制器调用的Web服务中检索到的json。 现在,为了显示json字符串,我已经这样做了 $.ajax({ url: this.href, type: 'GET', dataType: "json", data: { myPartNo: returnVal }, success: function (result) { ShowJson(result);
$.ajax({
url: this.href,
type: 'GET',
dataType: "json",
data: { myPartNo: returnVal },
success: function (result) {
ShowJson(result);
}
});
我只是将json字符串数据作为文本显示在div中(它可以工作),但基本上,我只需要json中的一些值,例如“color”和“size”。好的,像objectarray反序列化等词汇就是我需要帮助的地方。我可能在其他项目中做过,但不知道它叫什么。我需要做什么?在服务器端,您通常从控制器端或仅在javascript中定义一些DTO(数据传输对象),其中包含所有内容,如:
public class MyDTO
{
public string value {get; set;}
public string color {get; set;}
public int size {get; set;}
}
在控制器中,您只需将其包装为Json:
ActionResult MyController(int whatever)
{
MyDTO model = new MyDTO();
model.value = ...
return this.Json(model);
}
在客户端,您读取结果并将其视为常规对象,如:
ShowJson(result.color);
//或
谢谢凯特的来信。您知道result.value中的“…”部分在哪里,这是一个webservice,它的响应是一个json字符串,我不知道它的语法。或者我会在视图中使用jQuery.parseJSON吗?它只是关于服务器端的。抱歉搞混了,这只是关于模型的。您只需定义值,然后作为Json结果传递。如果还不清楚,请告诉我。
$("#mydiv").css("color", result.color); // for example