I've read a bunch of different articles, comparations and tutorials that are using different JSON-Libraries for parsing (and creating) JSON into Java Objects. Anyway I think that I've got the facts right cause I've decided to use the JSON library called Jackson.
GSON is simple and robust but way to slow acording to me. So I decided to actually try this Jackson thing out but, it seems like the parsing is a little bit more confusing here than with GSON.
The data-type of the value that I want to parse is simply an Boolean .
This is what the JSON that I'm trying to parse looks like:
{"FooResult":true}
So what I actually need help with is selecting the value from the key FooResult
and then parse its value into an Boolean.
This Is what I've done so far:
String json = getString(request);
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, Boolean.class);
But this code obviously gives me an error cause I haven't selected that it is the FooResult
key that I'm interested in reading & parsing into an Boolean.
You should create a new class like this:
class MyClass {
public boolean FooResult;
}
And use this code to load the data:
MyClass myObject = mapper.readValue(json, MyClass.class);
Then you can access the value with myObject.FooResult
Ok this is lame. Even lamer when I rethink about it. The problem the whole time have been that the class of the object that you want to parse needs to be static . I've tried what Simon suggested like four or five times before I even posted this question today but the problem all time was that the class wasn't static .
So now it finally works.
static class FooClass
{
public boolean FooResult;
}
And for the parsing process.
String json = getString(request);
ObjectMapper mapper = new ObjectMapper();
FooClass fooClass = null;
try
{
fooClass = mapper.readValue(json, FooClass.class);
}
boolean result = fooClass.FooResult;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.