-
11 Apr 2013 11:28 AM #1
Unanswered: How to get a JsonReader to read a single JSON Object?
Unanswered: How to get a JsonReader to read a single JSON Object?
All of the examples are for LIsts of data. I want to read a single JSON object and convert it to an AutoBean. I have tried without success for a while now. I have also googled around extensively.
How would I read in an object like:
I would even be happy with being able to read:Code:{ foo: 10, bar: "hello" }
Code:data: { foo: 10, bar: "hello" }
-
11 Apr 2013 12:20 PM #2
What have you tried? What does your autobean look like? Do you get any error messages?
Are you trying to read a single object like that, or a full list of them? Usually the DataReader api (and so JsonReader) is used to read lists on behalf of a Loader/Proxy, so when you need individual objects, you just use the AutoBeanCodex directly, calling decode with the right factory type, autobean type, and the string itself.
Note please that neither string is JSON, though the first is at least close. You must quote your key names:
Code:{ "foo": 10, "bar": "hello" }
-
11 Apr 2013 2:08 PM #3
I used a JasonWriter like this:
It has no "Base" parameter. It generates JSON without a root.Code:JsonWriter<System> writer = new JsonWriter<System>(factory,System.class);
I wanted to do the same, but JasonReader wants a "Base"
I was hoping to get a simple object with having to have a root node.Code:JsonReader<System, Base> reader = new JsonReader<System, Base>(_beanFactory, Base.class);
I would like to read in:
\Code:{ "foo": 10, "bar": "hello" }
I was able to get this to work:
P.S. I believe you need not quote the keys unless they are reserved keywords in JavaScript.Code:{ data: { "foo": 10, "bar": "hello" } }
Always quoting them is obviously safer, but not mandatory. The quotes do add to the payload size.
-
11 Apr 2013 2:53 PM #4
AutoBeanCodex worked like a champ!
Thank you very much for the quick response!
-
12 Apr 2013 7:10 AM #5
Glad to hear it worked.
JsonReader's Base generic arguement is not always required, as you noted. In that case, you must specify the Result argument twice. This is the default - createReturnData will just return the base object as if it were the result.


Reply With Quote