1

I'm pretty much new to jquery, i'm trying to read an JSON object.

My Jquery ajax call

$(document).ready(function () {

        var resource = "v1/projects";

        var url = '@Url.Action("Proxy")?resource=' + resource;

        var method = 'GET';


        var settings = {
            dataType: "text",
            type: method,
            success: function (data, textStatus, jqXHR) {

               var res= JSON.stringify(data);


               for (var k in res) {
                   alert(res)
               }

            },
            error: function (jqXHR, textStatus) {

                alert("error");
            }

        };



        $.ajax(url, settings);
    });

Ajax call works fine and i receive the following result.

enter image description here

I need to extract name and description from the result.

I tried using the for loop but it prints all the elements in the result

4 Answers 4

1

use . operator

var res=JSON.parse(data);
$.each(res,function(i,v){
    alert(v.name);
    alert(v.description);
});
Sign up to request clarification or add additional context in comments.

1 Comment

i would recommned you to use dataType: "json", and return as JSON from server ... that you you don't have to parse or stringify your json
1

Instead of JSON.stringify, you would want to use var res= JSON.parse(data) to convert it into a JS object and then use alert(res[k]) in the for loop

Comments

1

try this

for (var k in res) {
    alert(' name=' +k+ ' description=' +k+');
}

Comments

0

got it working with the following.

 success: function (data, textStatus, jqXHR) {

                     var obj = eval("(" + jqXHR.responseText + ")");

                      for (var k in obj.data) {
                        alert(obj.data[k].name);

                      }

                },

Comments

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.