Solved

JQuery: json array problem during web service development

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use "if not" in a condition 2 18
Link Anchor Not working 9 42
null, truthy, falsey confusion 25 28
Put javascript to get dropdown value 3 21
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-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

679 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