0

I am invoking a ASP.Net web-service (.asmx) using j-query and my response is a json array as follows

my jquery code is:

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
    url: "http://localhost:1000/WebSite2/Service.asmx/HelloWorld",
    data: '{}',
    success: function(msg) {
        alert(msg.d);
    },
    error: function() {  
        alert("Error");   
    }
});

my response is:

 [
       {
          "id":1,
          "name":"a",
          "place":"b"
       },
       {
          "id":2,
          "name":"c",
          "place":"d"
       },
       {
          "id":3,
          "name":"e",
          "place":"f"
       }
    ]

I want to add each objects in array to a list using j-query .I have tried a lot of ways but failed can anyone help me

5
  • Show us what you've tried, first of all. Commented Oct 31, 2012 at 4:32
  • 4
    there's no such thing as a json array. there's just json strings which can be parsed into a native data structure, which MAY be an array. you never work with json strings directly. it's just a data encapsulation/transmission format. You always work with native structures, then convert to json strings as needed. Commented Oct 31, 2012 at 4:32
  • 1
    What do you mean by a list here..?? Commented Oct 31, 2012 at 4:34
  • @Sushanth-- i am using jquery mobile . By list i mean a simple list view Commented Oct 31, 2012 at 4:44
  • @MarcB i am novice to JSON . My doubt is if their is no thing like JSONArray . JSON why the are sayin about JSONArray . JSON-JAVA we can also see this here Commented Oct 31, 2012 at 4:54

4 Answers 4

1

I have added the objects in the array to a ul ...

var data =[
   {
      "id":1,
      "name":"a",
      "place":"b"
   },
   {
      "id":2,
      "name":"c",
      "place":"d"
   },
   {
      "id":3,
      "name":"e",
      "place":"f"
   }
]​;

var html = '<ul>';

$.each(data, function(i) {
    html += '<li id="' + data[i].id + '">Name = '+ data[i].name + ' Place = ' + data[i].place + '</li>'; 
});
html += '</ul>';

$('#divList').append(html);

Replace the data with msg.d and it should be good..

Check Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

i tried this before but it doesn't work for me .actually while running the code the value of i is taken as 96 . i don't know what is the error done by me
May be you have declared a variable i in the same scope
Copy the code into your post so that it can be looked at by a fresh pair of eyes..
1

You have to convert json string to json object by using jquery.parseJSON. You can then use the attribute names of each array object.

Live Demo

var jsonString =  '[{"id":1,"name":"a","place":"b"},{"id":2,"name":"c","place":"d"}, {  "id":3,"name":"e","place":"f"}]';

jsonArray = $.parseJSON(jsonString );

for(i=0; i < jsonArray.length; i++)
{
    alert(jsonArray[i].id);
    alert(jsonArray[i].name);
    alert(jsonArray[i].place);
}

Comments

1

DEMO

var jsondata =[
{
  "id":1,
  "name":"a",
  "place":"b"
},
{
  "id":2,
  "name":"c",
  "place":"d"
},
{
  "id":3,
  "name":"e",
  "place":"f"
}
];


$.each(jsondata, function(key,val) {
 $.each(val, function(k,v){   
      alert(v);
 });
});

Comments

1

Something like this should work for you. No promises, but it's a start:

JS

success: function(msg) {
    var len = msg.d.length;
    var message = msg.d;
    for (var i=0; i < len; i++)
       {
        $('ul.container').append('<li id="' +message[i].id+  '" place="' +  message[i].place +  '">' +  message[i].name +  '</li>);
       }
},

HTML

<ul class="container">

</ul>

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.