Solved

JQuery: json array problem during web service development

Posted on 2011-09-16
5
361 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

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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