Solved

working with JSON data with multiple items returned

Posted on 2013-10-23
3
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

739 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