0

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

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
Sign up to request clarification or add additional context in comments.

2 Comments

thank you Kate for the post. You know where you have the "..." part in result.value, that's the webservice which response is a json string that I do not know the syntax for. Or would I use jQuery.parseJSON on the view?
It's only about server side. Sorry for confusing, it was only about model. You just define the value and then pass as Json result. Let me know if it's still unclear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.