Solved

JQuery: json array problem during web service development

Posted on 2011-09-16
5
360 Views
Last Modified: 2013-11-08
hello all,

i use a php client to read soap response message from a web service. The value i read will be transmit to javascript by using json.

the problem i met is:
let's say i got two variable from web service, and i would like to display them line by line.

 in firebugs the JSON looks like:

var01:
[Object{information="first one"}, Object{information="second one"}]

var02:
Object{information="third one"}

you see the different, var02 just have one Object, and he doesn't contain symbol [ ].

for var01, i can use following codes to display it on the screen:

$.each(var01, function(index, details){                                                    
     $.each(details, function(subindex, val) {                                
                   $("#divId").append(val);
                   $("#divId").append('</br>');
      });                            
 });

but for var02, if i use the codes above,  i got a wrong result, the output is "third one" but "t" "h" "i" "r" "d" " " "o" "n" "e"

the question is:
1: how do i show var02 correctly, the output excepte is "third one".
2: because in my project, the var01, var02 are depended on web service, i don't know when they will contain symbol [ ], when not.  Are there anyway to detect that
Object{information="third one"}
is not
[Object{information="third one"}] ?
3: How do i convert
Object{information="third one"}
to
[Object{information="third one"}] ?

thanks,
wantime
0
Comment
Question by:wantime
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 36548665
>1: how do i show var02 correctly, the output excepte is "third one".

$("#divId").append(var02);
$("#divId").append('</br>');    


>2: because in my project, the var01, var02 are depended on web service, i don't know when they will contain symbol [ ], when not.  Are there anyway to detect that
>Object{information="third one"}
>is not
>[Object{information="third one"}] ?

your webservice should not return randomly value. It must return the right type of value, not a surprise at each request
else you ay use :
var Im_A_String = ( typeof "third one" == "string"); // return true

>3: How do i convert
>Object{information="third one"}
>to
>[Object{information="third one"}] ?

var var02 = [var02];
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36549106
In short, You should have valid json. To check for valid json

var response=jQuery.parseJSON('response from server');
if(typeof reponse =='object')
{
  //It is JSON
}
else if(response ===false)
  {
     //the response was a string "false", parseJSON will convert it to boolean false. Convert string to json as @leakim971 said
  }
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 400 total points
ID: 36549163
>if(typeof reponse =='object')

it's preferable to use typeof response == "string'"

because :

typeof null return object
typeof [...] return object
typeof {..} return object
typeof obj return object
and so on...

if(typeof response =='string')
{
     //the response was a string "false", parseJSON will convert it to boolean false. Convert string to json as @leakim971 said
}
else if(response ===false)
  {
  //It is JSON
  }
0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 100 total points
ID: 36549171
You are true @leakim971. My mistake.
0
 

Author Comment

by:wantime
ID: 36549401
great. thanks a lot.
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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now