Solved

working with JSON data with multiple items returned

Posted on 2013-10-23
3
283 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 82

Accepted Solution

by:
leakim971 earned 400 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

786 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