• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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
0
wantime
Asked:
wantime
  • 2
  • 2
3 Solutions
 
leakim971PluritechnicianCommented:
>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
 
Kiran SonawaneProject LeadCommented:
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
 
leakim971PluritechnicianCommented:
>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
 
Kiran SonawaneProject LeadCommented:
You are true @leakim971. My mistake.
0
 
wantimeAuthor Commented:
great. thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now