Reputation: 61
I have searched many sites but couldn't find a solution to my problem. Basically my requirement is a bit tricky and some times I myself feel that it is not possible. Can anyone help me?
My requirement is I have 3 classes which are inherited from an interface. I now want to create a JSON array which has the following structure:
interface i {
}
class A implements i {
field l;
field 2;
}
First of all thanks for ur interest in solving my problem, All the objects which i mentioned in my questions a database persistence objects. I have contact object, communication object and address object, all are db persistence objects A single contact has multiple communications and multiple address. so, contact persists in a table and communications and addresses in 2 different tables, with reference to the unique identifier of the contact some thing like
contact table:
id firstname lastname
3 xyz abc
communication table:
id contactid number
1 3 999999999
adress table:
id contactid place 1 3 india
after persisting in database i want to create json array which i want to send in request to server.
final Collection<Syncable> collection = new ArrayList<Syncable>();
collection.add(contact);
collection.add(comm);
collection.add(address);
result = HttpRestUtil.post(url,*** new Gson().toJson(collection)***,JSON,header );
Gson().toJson(collection) returns a json in string, which i want to send in server side. the string looks is as follows
[{"firstname":"xyz","lastname":"abc"}, //contact
{"number":"99999999"}, //communication
{"place":"india"} //address
]
but my server is implemented which takes a json of the format
[ {"firstname":"xyz",
"lastname":"abc",
{"number":"999999999"}
{"place":"india"}
}]
server is coded as such the entire json is deserialized to objects of contact, communication, address
I hope i am clear in my question this time alteast once again thanks a lot
class B implements i {
field 3;
field 4;
}
class C implements i {
field 5;
field 6;
}
Now I want a JSON array which is of the following format, this JSON is of type A
[
"field1": "value",
"field2": "value",
"objectB": "{ 'field3':'value','field4':'value'}",
"objectC": "{ 'field5':'value','field6':'value'}"
]
My requirment is the B, C classes should be fields of the A class.
One method which can give me the solution is making B and C classes inner classes of A, but my object structure wont permit that. Help with this highly appreciated.
Thanks in advance
Upvotes: 1
Views: 471
Reputation: 61
i have not found any solution to my above problem, to get a solution i am forced to change my object structure now i got the json format which i required.
I have changed my contact object to contains the communication and address as fields
public class Contact
{
String firstName;
String lastName;
Communication comm[];
Address address[];
}
communication and Address are two different classes
after setting the values of Contact object
new Gson().toJson(contact);
gives me the json string i required
reference:
http://en.wikipedia.org/wiki/GSON
Upvotes: 0
Reputation: 7871
I have a few questions/comments:
If your JSON is meant to represent your A class, it should be an object, not an array. Switch the brackets to curly braces.
If instances of B and C are stored in fields of an A instance, you should update your class model to reflect that. Currently, they are just three distinct classes. All your fields are strings, based on your posted JSON. It would help if you add type information to the fields in the class declarations.
It wouldn't hurt to post the code you're using to generate the JSON.
Upvotes: 2