I do have a JSON String request which uses JAXB to convert to number instead of fields
"{\"1\":{\"3\":\"788888\",\"2\":\"12345\",\"0\":{\"0\":\"Login\",\"1\":\"xx\",\"2\":\"yy\",\"3\":\"\",\"4\":\"1\",\"5\":\"hhh\"}}}"
And the response which we get is also in the above format
Is there any way to convert JSON String Response to Object where the object instance should be field values instead of numbers(keys)?
Note:-I had annotated(JAXB) fields in Request/Response class?
Related
I have a post rest api in spring boot. I am sending the password string in json payload for this api. While deserializing using jackson, I am using the String field in Request Dto class. Since, it is not secure I would like to know if there is way to deserialize the password string in json to guarded string implementation.
Guarded String Url : https://docs.oracle.com/html/E28160_01/org/identityconnectors/common/security/GuardedString.html
Or is. there a way to deserialize the string json to char array using jackson annotations.
I am trying to to convert JSON to POJO class. This JSON I am getting from third party REST API call and I want to convert it into POJO class. For this I am using jackson-databind jar and below is part of my code.
ObjectMapper mapper = new ObjectMapper();
Object modelObject; // object in which I want to convert my JSON object
mapper.writeValue(request.getShipmentDataJson(), modelObject);
Here for now instead of POJO class I declared modelObjcet variable of Object type and my question is do we need to create POJO class with required fields and getter setter methods before converting JSON to POJO?
If yes, then how should we create this POJO class from JSONSchema and when it get created?
Please explain me this concept. My understanding is we POJO should get create directly from JSONSchema but when and how that I don't know. And I think once POJO get created then I can use my above code to store JSON object to POJO.
You need an object with fields corresponding to incoming JSON (names and datatypes) - so jackson can populate and instantiate it. There are tools like this:
http://www.jsonschema2pojo.org/
to generate java code from JSON
I am attempting to convert JSON into a Java object with the Play framework. I do not have easy control over the input JSON, which contains dashes in the names.
{ "field-name": "value" }
As a result, I cannot create a Java object with a default mapping to the JSON. I have a class which looks like this:
import com.fasterxml.jackson.annotation.JsonProperty;
public class Data {
#JsonProperty("field-name")
public String fieldName;
}
I know that Play 2.4 uses Jackson, and
I have a unit test which is able to populate the object from the JSON using a default Jackson ObjectMapper.
The JSON is the body of a POST request, and I attempt to use it like this:
Form<Data> form = Form.form(Data.class).bindFromRequest();
If I print form, I can that the data field is populated with the expected values. However, when I do form.get(), the returned value has a null field. (In the actual code, there are more fields, which are Strings or longs. All of them are null or 0.)
Am I attempting to customize the JSON deserialization in the wrong way? Or am I doing something else wrong?
As you've expected you've used the wrong way to deserialize. The Forms class is for PlayForms only and not for Json request. Have a look at the BodyParser and JsonActions documentation:
#BodyParser.Of(BodyParser.Json.class)
public Result index() {
RequestBody body = request().body();
Data data = Json.fromJson(body.asJson(), Data.class);
return ok("Got java object: " + data.toString());
}
I'm using Jersey (2.5.1) for a RESTish API with JAXB to marshal JSON to/from POJOs. The client will be doing a POST with the following request:
{
"type":"myevent",
"data":{
"id":"123",
"count":2
}
}
I have an 'Event' class which holds a type string and a data payload.
#XmlRootElement
public class Event {
#XmlElement public String type;
#XmlElement public JSONObject data;
...
}
The 'data' payload is a JSON object, however I don't know what type, or what the 'schema' of the object is. All I know is it's JSON. Above I have the type as a JSONObject, but that's just an example, maybe this needs to be Object? Map? Something else?
I want to be able to get the 'data' payload and persist this as JSON somewhere else.
I thought about using a String for the data payload, but then any API client would need to encode this and I would need to decode it before passing it on.
Any suggestions?
I usually work with strings on the backend side and then
JSONObject json = new JSONObject(s);
would create a json obj from that s (you don't need to decode).
On the client side I believe you just need to escape the " with something like a replaceAll function applied on that string
I am trying to parse JSON data being sent from UI in my Controller using Spring build Jackson support and this is my code
final Map<String, CartDataHelper> entriesToUpdateMap = new ObjectMapper().readValue(entriesToUpdate, new TypeReference<Map<String, CartDataHelper>>()
my JSON string is
{"0":"{\"categoryCode\":\"shoes\",\"productCode\":\"300050253\",\"initialQty\":\"3\",\"leftoverQty\":\"0\",\"newQty\":\"3\"}",
"1":"{\"categoryCode\":\"shoes\",\"productCode\":\"300050254\",\"initialQty\":\"3\",\"leftoverQty\":\"0\",\"newQty\":\"3\"}"}
i checked the JSON format using some online services and it seems valid, while tryin gto parse JSON data i am getting following exception
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class controllers.util.CartDataHelper] from JSON String; no single-String constructor/factory method
my CartDataHelper class contains simple properties for for productCode, categoryCode etc with a no argument constructor
As comments mentioned, your JSON contains Map<String,String> and NOT Map<String,CartDataHelper>: values are JSON Strings, not JSON Objects.
Ideally you would not try writing out objects as JSON Strings; and if so, things would work.
It seems that on the client side the json is sent as a string instead as an object. That way on the server side you are receiveing a string and not a CartDataHelper as you pretend.
Try sending JSON.parse(stringCartDataHelper). It worked for me with the same issue.