I am getting following JSON string through http webservice:
Raw JSON String:
[
{
field1OfClass1:"someValue",
field2OfClass1:"someValue",
field1OfClass2:"someValue",
field2OfClass3:"someValue"
}
]
Classes:
class Class1
{
String field1;
String field2;
}
class Class2
{
String field1;
}
class Class3
{
String field2;
}
In GSON is there any way to parse above said JSON string with the fields of depicted classes?
Thanks
As best I know, there is no built-in feature of Gson to automagically map the example JSON structure to the example Java data structure in the original question. Custom deserialization processing is necessary.
Related
PostMapping method
#RestController
#RequestMapping("/validate")
public class Validatesimapi {
#PostMapping
public Simoffers validateSim(#RequestBody ???)
}
I want to pass following json object through post request and accept it in validateSim. What should I write at ???.
{
"id": "1234",
"num":"2343335"
}
both the datatypes of id and num is String.
enter code here
It’s as simple as adding a DTO with the fields that you want. The Jackson mapper will map the json to that object.
I am new to groovy Grails and trying to understand how to work with GORM
We have SQL table with column of string type that holds JSON String representing some object
(I can't alternate db design)
I understand that in groovy Model objects represent SQL records and in general we can use marshallers to render objects to JSON
But what I need is to get, create or save Model object that have Json string column that will be rendered to an object in groovy, but can't find any information on how to do it
for example to simplify i will have following table : id(number), json(longstring)
and in JSON:
{"name":"object1", "list":[{"item":"item1", "type":"type1"},{"item":""item2", "type":"type2"},..]}
and following classes:
class MainModelClass {
Long id
MyObject o
...
}
class MyObject {
List<Item> items
...
}
class Item {
String item
String type
...
}
How can I make the Model object parse the JSON to Object structure
Thanks
You could use a simple trick with a transient property like so:
import groovy.json.*
class MainModelClass {
String originalJson
static final JsonSlurper slurper = new JsonSlurper()
MyObject getMyObject(){
slurper.parseText( originalJson ) as MyObject
}
void setMyObject( MyObject myObject ){
originalJson = JsonOutput.toJson myObject
}
static transients = [ 'myObject' ]
}
You might want to use Jackson Mapper to have finer control over marshalling.
I have come across a problem of parsing json data . I am building project using spring boot based on REST api . When i have to parse data corresponding to domain then it is very easy , i use RequestBody in controller method with domain name but in current scenerio i have a list of domain in json form :
{
"data":[
{
"type":"abc",
"subtypes":[
{
"leftValue":"BEACH",
"rightValue":"MOUNTAIN",
"preferencePoint":60
},
{
"leftValue":"ADVENTURE",
"rightValue":"LEISURE",
"preferencePoint":60
}
]
},
{
"type":"mno",
"subtypes":[
{
"leftValue":"LUXURY",
"rightValue":"FUNCTIONAL",
"preferencePoint":60
},
{
"leftValue":"SENSIBLE",
"rightValue":"AGGRESIVE",
"preferencePoint":0
}
]
}
]
}
I am sending data in list where type is the property of class Type
and class Type has list of Subtypes class and subtype class contains leftValue and rightValue as enums
I am using spring boot which uses jackson liberary by default and i want to parse this data into corresponding Type class using Jackson. Can any one provide me solution.
It wasn't clear to me if you have static or dynamic payload.
Static payload
For static one, I would personally try to simplify your payload structure. But your structure would look like this. (I skipped getters and setters. You can generate them via Lombok library).
public class Subtype{
private String leftValue;
private String rightValue;
private int preferencePoint;
}
public class Type{
private String type;
private List<Subtype> subtypes;
}
public class Data{
private List<Type> data;
}
Then in your controller you inject Data type as #RequestBody.
Dynamic payload
For dynamic payload, there is option to inject LinkedHashMap<String, Object> as #RequestBody. Where value in that map is of type Object, which can be casted into another LinkedHashMap<String, Object> and therefore this approach support also nested objects. This can support infinite nesting this way. The only downside is that you need to cast Objects into correct types based on key from the map.
BTW, with pure Spring or Spring Boot I was always able to avoid explicit call against Jackson API, therefore I don't recommend to go down that path.
I have an interface Employee and Department. I'm loading JSON from server that I need to "parse" to object that implements those interfaces. Is there a way how to achieve this automatically since all types in interface and JSON object are base types (String, number, list, map)?
// Abstract classes represents interfaces
abstract class Employee {
String firstName;
String lastName;
}
abstract class Department {
String name;
List<Employee> employees;
}
// JSON
{
"name": "Development",
"employees":
[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
I want to parse it like this
main() {
...
Department department = someMethodToParse(jsonFromServer);
...
}
There are a few packages that deal with JSON de/serialization
http://www.dartdocs.org/documentation/serialization/0.9.1+1/index.html#serialization/serialization (serialization package)
Can I automatically serialize a Dart object to send over a Web Socket? (package exportable)
Convert JS object into Dart classes (manual)
How to convert an object containing DateTime fields to JSON in Dart? (handling DateDime)
Add JSON serializer to every model class?
I have an EntityId class that servers as a simple wrapper class to database identifiers. The class already has methods for converting to and from a string representation. I'd like to use this string representation of the EntityId in my JSON web resources.
What's the simplest to implement custom serialization for this simple type in Jackson? I know I can write a custom serializer and deserializer, but I wondered if there might be an even simpler solution.
Thanks!
If there is a method to serialize type as String, you can just add #JsonValue annotation like so:
public class MyClass {
#JsonValue public String toString() { return "xxx"; }
}
Conversely, if there is a single-arg constructor that takes a String, int or long (or some Java type that Jackson can convert to from JSON Scalar type), you can add #JsonCreator annotation next to that constructor:
public class MyClass {
#JsonCreator
public MyClass(OtherPojo value) { // or use 'Map<String,Object>', extract data
// ...
}
}