File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Struct array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Struct array" Watch "Struct array" New topic
Author

Struct array

Prasath Nadarajah
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi,
i,m developing a web service client.
Through that i get an Object array with the following struct
array
struct
boolean isAdmin
string url
string blogid
string blogName
string xmlrpc

how can i retrieve values from this???
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

you get an Object array, and in there are different types?

If you have the option, I'd say re-write it to return something other than an Object array.

If you are stuck with it, will the structure EVER change? Can you be sure that element 4 (or whatever) will ALWAYS be your blogid? you could hardcode it so that you set each element to a correct reference:

String myBlogID = objectArray[4];

etc.

Note - please try and be precise when explaining the problem. There no such thing as a 'string' in java (unless you wrote that class yourself). There is a String class. these details are important.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Prasath Nadarajah
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi fred!!

This is my actual code and it works fine..
I get object in return . but i don,t know how to retrieve values from it..
for example i want to print the blogName.
see this link for the structure of the object returned..


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Please use the code button. Since nobody else has pointed it out to you, I shall edit your post and you can see how much better it looks

And welcome to the Ranch

By the way: what's a struct? I have seen it in C, but never in Java™. Can you get your output into a single Blog object?
Prasath Nadarajah
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Thanks for the edit.
me too not familiar with struct in java.
The return value is a object with the specified parameters..
I don,t knw how to retrieve values from it??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

I'd start by reading the documentation. I've spent a few minutes googling, and here is what i THINK is happening...

your basically sending the command "wp.getUsersBlogs" to somewhere. gooling that string indicates you get an Object array back from that call.

I think element [0] will be your boolean answering if the userid isAdmin
element [1] will be your String holding the URL


so i'd pick each element off and assign them to the correct type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
If you get an Object[], you can iterate the arrayThat will give you some hints to get you started.
Prasath Nadarajah
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Actually the return data type was an array of objects which has a hash table inside it.
So first you get the result and cast it to an object array.

Then get the first element of that object array and cast it to a hash table

Then get the element by giving the hash key.
So the final code looks like this

and it prints the blogName BINGO!!!

I do'nt get why the array should be casted to an object array??
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Nadarajah,

Well an Object can be anything, including an Object[] (an Object[] cannot be cast to an Object though).

In your case, the variable result was declared of type Object, and so you could skip the casting in this line:

Object result = client.execute("wp.getUsersBlogs", params);

And then, after you stored the result of client.execute in a variable which can store any type, you're like, ok I know that the Object is actually an Object[] so do this:

Object[] resultProper = (Object[])result;

But you could have just as easily done this:

Object[] result = (Object[]) client.execute("wp.getUsersBlogs", params);

I do'nt get why the array should be casted to an object array??


Get it now? There was no array to cast to an object array as far as the compiler is concerned (even though YOU knew the Object result had an Object[], the compiler only knew that the variable result was of type Object and therefore could hold any type).
Then, since YOU knew that it actually is an Object[], you told the compiler "hey, that Object is actually an Object[]", and you did this by casting.

Hope that helps.

Justin
 
Consider Paul's rocket mass heater.
 
subject: Struct array
 
Similar Threads
Call to Oracle SP with Oracle object type parameter
assign the two dimension array in struct jsp page
struct in java
struct in java
JNI help needed: adding jstrings to jobjectArray