Skip to content
Advertisement

Java parse JSON path value to variable

I have a problem. I had the following code to parse a JSON string to an object:

JavaScript

But now I have a JSON with values that are not equal to the attribute name. Here is the new JSON:

JavaScript

And this is what the class looks like:

JavaScript

Now how can I match the JSON and class as the following:

JavaScript

Advertisement

Answer

You can write a custom deserializer in which you use default Gson deserialization and additionally manually parse the “problematic” fields:

JavaScript

This is how you use the deserializer:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement