I am trying to parse the json retrieved from a webservice called from my controller. For now, just to show the json string, I've done this
$.ajax({
url: this.href,
type: 'GET',
dataType: "json",
data: { myPartNo: returnVal },
success: function (result) {
ShowJson(result);
}
});
I just have the json string data displayed in a div as text (it works) but basically, I just want a few of the values from that json like "color" and "size" for example. Ok, so the vocabulary words like object array deserialize etc is where I need help. I've probably done it in other projects without knowing what it is called. What do I need to do? From the controller end or just within javascript?
1 Answer 1
On the server side you usually define some DTO (data transfer object) that has everything inside like:
public class MyDTO
{
public string value {get; set;}
public string color {get; set;}
public int size {get; set;}
}
In your controller you just wrap it into Json:
ActionResult MyController(int whatever)
{
MyDTO model = new MyDTO();
model.value = ...
return this.Json(model);
}
On the client side you read the result and treat it as a regular object like:
ShowJson(result.color);
//or
$("#mydiv").css("color", result.color); // for example