Link to home
Start Free TrialLog in
Avatar of wantime
wantime

asked on

JQuery: json array problem during web service development

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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
  }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wantime
wantime

ASKER

great. thanks a lot.