PDA

View Full Version : Loading complex JSON data



catalin.ciobanu
23 Sep 2011, 2:01 AM
Hi guys, I use JSON to load data and I can do it but without 1 element of it.



{"docodel":[{
"nome":"ssss",
"note":"no_note",
"oggetto":"Object_dwfa",
"properties":{
"numero":"Number_sssda"
},
"uuid":"1fd1cf79-8709-48be-b759-b53829c1b86c"
},
{
"nome":"ssss",
"note":"some note",
"oggetto":"Ca1Obj",
"properties":{
"numero":"CinNumber"
},
"uuid":"757c0a49-95e5-4656-99f8-99c17976132a"
},
{
"nome":"Cat23",
"note":"som211",
"oggetto":"CObjOsk",
"properties":{
"numero":"NumeroAlto"
},
"uuid":"aec5895e-3f65-4b4a-9f99-5216d0a7c082"
}]
}

Using this modeltype I manage to take all the data but the "properties" I cannot take.

ModelType type = new ModelType();
type.setRecordName(null);
type.setRoot("docodel");
type.addField("nome", "nome");
type.addField("note", "note");
type.addField("oggetto", "oggetto");
type.addField("properties", "properties");
type.addField("uuid", "uuid");

MemoryProxy<ModelData> proxy = new MemoryProxy<ModelData>(jsonData);
JsonReader<List<ModelData>> reader = new JsonReader<List<ModelData>>(
type);

loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);
ListStore<ModelData> store = new ListStore<ModelData>(loader);

loader.load();


How can I access that specific member which is a JSON Object ? Thanks a lot !

sven
23 Sep 2011, 2:05 AM
JsonReader by default does not support reading of nested values, like your "properties" are. You will need to create a subclass to add this feature.

When you look at the JsonReader code right now, you will see that it is doing "nothing" when it finds an object.

catalin.ciobanu
23 Sep 2011, 2:32 AM
Thanks mister, it works ... and it isn't that difficult either.

gxtdev11
10 Nov 2011, 8:54 AM
Can you please share the solution? I am facing similar problem.

catalin.ciobanu
10 Nov 2011, 8:58 AM
I overriden the JSONReader
and
"When you look at the JsonReader code right now, you will see that it is doing "nothing" when it finds an object."

now he does return the OBJECT in stead of "doing nothing"

gxtdev11
10 Nov 2011, 8:03 PM
Thanks. Would it be possible for you to send that piece of code? That would be really helpful.

Thank for your help..

catalin.ciobanu
11 Nov 2011, 12:52 AM
ok, in JSONReader class, method @read, you have



.........................................
} else if (value.isObject() != null) {
// nothing
} else if (value.isString() != null) {
..........................................................

You can replace that with


.........................................
} else if (value.isObject() != null) {
return value;
} else if (value.isString() != null) {
..........................................................


now you have your JSONObject to do whatever you want with it ... you can also return the String representation, you can also handle the object and return the fields you are interested in ... or you can simply return the object and take the values you want, in the client code, as you have access to the JSON package in gxt

gxtdev11
11 Nov 2011, 5:14 AM
Thanks for your help. This was very helpful.