[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

working with JSON data with multiple items returned

Posted on 2013-10-23
3
Medium Priority
?
291 Views
Last Modified: 2013-10-24
I'm trying to return multiple items in my JSON call and place that data into multiple div's

This is my code to return the Json

     
 string JSONstring = "[";
        foreach (PersonInformation PS in PList)
           {
               JSONstring = JSONstring + "{";
               JSONstring = JSONstring + "EmpName:";
               JSONstring = JSONstring + "'" + PS.EmpName + "',";
               JSONstring = JSONstring + "PhoneNumber:";
               JSONstring = JSONstring + "'" + PS.Number + "'";
               JSONstring = JSONstring + "}";
           }
        JSONstring = JSONstring + "]";

Open in new window


This is my jquery

 $(document).ready(function () {
             $("#<%=txtEmployeeID.ClientID%>").blur(function () {
                 currentObject = $("#<%=txtEmployeeID.ClientID %>").val();
                 $.ajax({
                     type: "POST",
                     url: "EmployeeData.asmx/GetEmployeeInfo",
                     data: "{employeeID:'" + currentObject + "'}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function (msg) {
                         BindEmployee(msg.d)
                     },
                     error: function (xhr, ajaxOptions, thrownError) {
                         alert(xhr.status);
                         alert(thrownError);
                     }

                 });

             });

             function BindEmployee(msg) {
                 $.each(msg, function () {
                     var EmpName = msg.d[0];
                     var Phone = msg.d[1];
                     $("#EmployeeName").html(EmpName);
                     $("#PhoneNumber").html(Phone);

                 });
             }
        });

Open in new window


what am I doing wrong?
0
Comment
Question by:NickMalloy
3 Comments
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 39595476
You haven't told us what isn't working - meaning are you getting an error message, etc.

That said, take a look at your output.  There's probably no comma in between the objects.  Not sure what language that is, but can you build an array and use some sort of join function?  Otherwise, if you put the comma at the end of your foreach, you'll end up with a final comma, which will screw things up.
0
 

Author Comment

by:NickMalloy
ID: 39595517
It returns a string, but I can't break it up into my fields. I'm working on a asp.net c# page using jquery.

My response from my ajax call is this.

"[{\"EmpName\":\"Nick Malloy\",\"Phone\":\"96325\"}]"
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1600 total points
ID: 39595646
Test page : http://jsfiddle.net/yjpJq/

          var a = "[{\"EmpName\":\"Nick Malloy\",\"Phone\":\"96325\"}]";
          a = $.parseJSON(a);
          BindEmployee(a);

            function BindEmployee(msg) {
                     var EmpName = msg[0].EmpName;
                     var Phone = msg[0].Phone;
                     $("#EmployeeName").html(EmpName);
                     $("#PhoneNumber").html(Phone);
             }

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question