Solved

working with JSON data with multiple items returned

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.net mvc 5 5 45
Angular- another way of coding hide and display 3 41
Javascript create file issue 4 49
Error in script 11 48
by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
The viewer will learn how to dynamically set the form action using jQuery.
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)

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now