How to read this JSON as an object? Gson - java

I have a decent amount of experience with REST and JSON, but I'm failing at coming up with a way to read some JSON as a Java object.
The response is here: https://api.kraken.com/0/public/OHLC?pair=XBTCZEUR&interval=60
Notice how one of the names (the relevant data) is dependent on a query parameter. I'm not sure how to create a Java object for Gson to use for deserialization, as one of the variable names can change.
I thought that maybe using a JsonReader to read the response in a streaming fashion might work, but when I do this I get a 403 error response.
Any ideas?

If you don't have exact knowledge regarding what the response will contain, you can always use an implementation of map class to pass on to gson, as I have tried to demonstrate in here :
public class RestResponse {
private boolean success;
private String errorDescription;
private Map<String, Object> data;
private static Gson GSON = new Gson();
private RestResponse()
{
data = new HashMap<String, Object>();
}
public boolean isSuccess() {
return success;
}
private void setSuccess(boolean success) {
this.success = success;
}
public String getErrorDescription() {
return errorDescription;
}
private void setErrorDescription(String errorDescription) {
this.errorDescription = errorDescription;
}
public Object getData(String... nestedKeys)
{
List<String> nestedKeysAsList = Arrays.asList(nestedKeys);
return getData(nestedKeysAsList);
}
public Object getData(List<String> nestedKeys)
{
String firstKey = nestedKeys.get(0);
if(!data.containsKey(firstKey))
throw new IllegalArgumentException("Key not found");
Object mapValue = data.get(firstKey);
if(!(mapValue instanceof Map))
return mapValue;
String finalKey = nestedKeys.get(nestedKeys.size()-1);
if(nestedKeys.size() > 2)
{
for(String nextKey : nestedKeys.subList(1,nestedKeys.size()-1))
{
Map<String,Object> tempMap = (Map)mapValue;
mapValue = tempMap.get(nextKey);
}
}
Map<String,Object> tempMap = (Map)mapValue;
return tempMap.get(finalKey);
}
private Map<String, Object> getData() {
return data;
}
private void setData(Map<String, Object> map){
this.data = map;
}
public static RestResponse createUnsuccessfulResponse(Exception e)
{
return createUnsuccessfulResponse(e.getMessage());
}
public static RestResponse createUnsuccessfulResponse(String reason)
{
RestResponse res = new RestResponse();
res.setSuccess(false);
res.setErrorDescription(reason);
return res;
}
public static RestResponse createSuccessfulResponse(String jsonString)
{
Map<String, Object> jsonToDataMap = GSON.fromJson(jsonString, Map.class);
return createSuccessfulResponseByMap(jsonToDataMap);
}
private static RestResponse createSuccessfulResponseByMap(Map<String, Object> jsonToDataMap)
{
RestResponse res = new RestResponse();
res.setSuccess(true);
res.setErrorDescription("Success");
res.setData(jsonToDataMap);
return res;
}
}
Usage examples can be found over here :
https://github.com/cgunduz/btcenter/blob/master/src/main/java/com/cemgunduz/utils/entity/RestResponse.java

Related

Return response from API as inner JSON objects

I use this code to get a list of countries as full name and ISO code:
public Map<String, Object> getCountryNameCodeList() {
String[] countryCodes = Locale.getISOCountries();
Map<String, Object> list = new HashMap<>();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
list.put(obj.getDisplayCountry().toString(), obj.getCountry());
}
return list;
}
Rest API:
#GetMapping("shipping_countries")
public ResponseEntity<Map<String, Object>> getShippingCountries() {
return new ResponseEntity<Map<String, Object>>(countriesService.getCountryNameCodeList(), HttpStatus.OK);
}
I get the response data in this format:
{
"Papua New Guinea": "PG",
"Cambodia": "KH",
"Kazakhstan": "KZ",
"Paraguay": "PY",
.....
}
I would like to get the data this way:
[
{
name: "Papua New Guinea",
value: "PG"
},
{
name: "Unites States",
value: "US"
},
....
]
How I can modify the Java code to return the data this way?
Try this approach. You need to use data transfer object to return customized data.
Create a class DTO.
public class DTO {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#Override
public String toString() {
return "DTO [key=" + key + ", value=" + value + "]";
}
}
Create Rest API in the controller. Example :
#RestController
public class Sample {
#RequestMapping("shipping_countries")
public ResponseEntity<List<DTO>> getShippingCountries() {
Map<String, String> map = new HashMap<>();
map.put("Papua New Guinea", "PG");
map.put("Cambodia", "KH");
List<DTO> list = getCustomisedData(map);
return ResponseEntity.ok(list);
}
private List<DTO> getCustomisedData(Map<String, String> map) {
List<DTO> dtos = new ArrayList();
for(Entry<String, String> value: map.entrySet()) {
DTO dto = new DTO();
dto.setKey(value.getKey());
dto.setValue(value.getValue());
dtos.add(dto);
}
return dtos;
}
}
Output :
The response you are getting is the JSON representation of a map, which is what you return.
The json you want is an array of objects, so if you want to return that- the easiest way will be to return it like that, is to return the set of Map.Entry from your map. Something like that:
#GetMapping("shipping_countries")
public ResponseEntity<Set<Map.Entry<String, Object>>> getShippingCountries() {
return new ResponseEntity<>(countriesService.getCountryNameCodeList().entrySet(), HttpStatus.OK);
}
Other way can be to create a Json serializer for the response, but it seems like an overkill

In java how to add nested map object

I am having a JSON data and I am converting that payload into a map object of nested. But it is overriding according to my logic.
I am having input json like this
{"mapping": {
"EVENT.alertMessage": "input.Message",
"EVENT.id": "input.id",
"EVENT.severity": "Functions.toString(\"P1\")",
"EVENT.eventTime": "input.eventTime",
"EVENT.eventType": "input.alertType",
"EVENT.geocoordinates.location": "Functions.toString(\"\")",
"EVENT.deviceName": "Functions.toString(\"\")",
"EVENT.visualInfo.imageUrl": "input.imageUrl",
"EVENT.deviceId": "input.cameraId",
"EVENT.geocoordinates.longitude": "Functions.toString(\"\")",
"EVENT.visualInfo.videoUrl": "input.videoUrl",
"EVENT.tenantCode": "Functions.toString(\"\")",
"EVENT.MAC": "input.cameraId",
"EVENT.DATE_TIME": "Functions.currentDate(\"yyyy-MM-dd HH:mm:ss\",\"UTC\")",
"EVENT.geocoordinates.latitude": "Functions.toString(\"\")"
}
}
Here from the above input JSON Keys I am iterating and forming map object.
ForEx:
INPUT:
{"mapping": {
"TEST.key1": "a",
"TEST.key2.key3": "b",
}
}
OUTPUT:
{
"TEST":{
"key1":a,
"key2":{
"key3":b
}
}
}
The code that I have written is
JSONObject json=new JSONObject(mappingData).getJSONObject("mapping");
Iterator<String> keys=new JSONObject(mappingData).getJSONObject("mapping").keys();
Map<String,Object> map = new HashMap<>();
while(keys.hasNext()) {
String val = keys.next();
String[] key=val.split("(?<!/)\\.");
Map<String, Object> lastKeyMap = null;
for(int i=0;i<key.length;i++)
{
if(i== 0 && key.length==1){
String outputVal=json.getString(val);
if(outputVal.contains("[]")){
outputVal=outputVal.replace("[]", "[i]");
}
//Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])|([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])")
// .matcher(outputVal);
Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9]{0,})")
.matcher(outputVal);
while (m.find()) {
outputVal=m.replaceAll("[`$1`]").replace("/", "");
}
if(key[i].contains("/"))
{
map.put("`"+key[i].replace("/", "")+"`",outputVal);
}
else{
map.put(key[i],outputVal);
}
}
else if(i== 0 && key.length>1){
if(map.containsKey(key[i])){
lastKeyMap = (Map<String, Object>) map.get(key[i]);
}else{
if(key[i].contains("/"))
{
lastKeyMap = new HashMap<String,Object>();
map.put("`"+key[i].replace("/", "")+"`",lastKeyMap);
}
else{
lastKeyMap = new HashMap<String,Object>();
map.put(key[i],lastKeyMap);
}
}
}else if(i== key.length-1 ){
String outputVal=json.getString(val);
if(outputVal.contains("[]")){
outputVal=outputVal.replace("[]", "[i]");
}
//Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])|([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])")
// .matcher(outputVal);
Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9]{0,})")
.matcher(outputVal);
while (m.find()) {
outputVal=m.replaceAll("[`$1`]").replace("/", "");
}
if(key[i].contains("/"))
{
lastKeyMap.put("`"+key[i].replace("/", "")+"`", outputVal);
}
else{
lastKeyMap.put(key[i], outputVal);
}
}else{
Map<String,Object> objMap = new HashMap<>();
if(key[i].contains("/"))
{
lastKeyMap.put("`"+key[i].replace("/", "")+"`", objMap);
lastKeyMap = objMap;
}
else{
lastKeyMap.put(key[i], objMap);
lastKeyMap = objMap;
}
}
}
}
The output I am getting is :
{EVENT={severity=Functions.toString("P1"), alertMessage=input.alertMessage, id=input.id, eventTime=input.eventTime, visualInfo={videoUrl=input.videoUrl}, eventType=input.alertType, tenantCode=Functions.toString(""), DATE_TIME=Functions.currentDate("yyyy-MM-dd HH:mm:ss","UTC"), geocoordinates={latitude=Functions.toString("")}, deviceName=Functions.toString(""), deviceId=input.cameraId, MAC=input.cameraId}}
But in the result EVENT.geocoordinates.longitude and EVENT.geocoordinates.longitude is skipped as the map is being overridden. Like that EVENT.visualInfo.imageUrl is also overridden by EVENT.visualInfo.videoUrl.So, how can I overcome this one and form a map or json with all the json keys by iterating without veing overriden.
The best approach is to create java class according to json schema:
public class Test {
#SerializedName("mapping")
public Mapping mapping;
static public class Mapping {
#SerializedName("EVENT.alertMessage")
public String alertMessage;
#SerializedName("EVENT.id")
public String id;
#SerializedName("EVENT.severity")
public String severity;
#SerializedName("EVENT.eventTime")
public String eventTime;
#SerializedName("EVENT.eventType")
public String eventType;
#SerializedName("EVENT.geocoordinates.location")
public String location;
#SerializedName("EVENT.deviceName")
public String deviceName;
#SerializedName("EVENT.visualInfo.imageUrl")
public String imageUrl;
#SerializedName("EVENT.deviceId")
public String deviceId;
#SerializedName("EVENT.geocoordinates.longitude")
public String longitude;
#SerializedName("EVENT.visualInfo.videoUrl")
public String videoUrl;
#SerializedName("EVENT.tenantCode")
public String tenantCode;
#SerializedName("EVENT.MAC")
public String mac;
#SerializedName("EVENT.DATE_TIME")
public String dateTime;
#SerializedName("EVENT.geocoordinates.latitude")
public String latitude;
}
}
And then parse it with google gson library
Test test = new Gson().fromJson("jsonString", Test.class);
Working with your own java object is much easier than with JSONObject
My current dependency for gson in gradle file:
implementation("com.google.code.gson:gson:2.8.6")

Building a Json String in java?

I am trying to build a json string in java but I am a bit confused as how I should go about it. This is what I tried so far.
String jsonString = new JSONObject()
.put("JSON1", "Hello World!")
.put("JSON2", "Hello my World!")
.put("JSON3", new JSONObject()
.put("key1", "value1")).toString();
System.out.println(jsonString);
The output is :
{"JSON2":"Hello my World!","JSON3":{"key1":"value1"},"JSON1":"Hello World!"}
The Json I want is as follows :-
{
"data":{
"nightclub":["abcbc","ahdjdjd","djjdjdd"],
"restaurants":["fjjfjf","kfkfkfk","fjfjjfjf"],
"response":"sucess"
}
}
How should I go about it?
You will need to use JSONArray and JsonArrayBuilder to map these json arrays.
This is the code you need to use:
String jsonString = new JSONObject()
.put("data", new JSONObject()
.put("nightclub", Json.createArrayBuilder()
.add("abcbc")
.add("ahdjdjdj")
.add("djdjdj").build())
.put("restaurants", Json.createArrayBuilder()
.add("abcbc")
.add("ahdjdjdj")
.add("djdjdj").build())
.put("response", "success"))
.toString();
You can use gson lib.
First create pojo object:
public class JsonReponse {
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public class Data {
private String reponse;
private List<String> nightclub;
private List<String> restaurants;
public String getReponse() {
return reponse;
}
public void setReponse(String reponse) {
this.reponse = reponse;
}
public List<String> getNightclub() {
return nightclub;
}
public void setNightclub(List<String> nightclub) {
this.nightclub = nightclub;
}
public List<String> getRestaurants() {
return restaurants;
}
public void setRestaurants(List<String> restaurants) {
this.restaurants = restaurants;
}
}
}
and next complite data and generate json:
JsonReponse jsonReponse = new JsonReponse();
JsonReponse.Data data = jsonReponse.new Data();
data.setReponse("sucess");
data.setNightclub(Arrays.asList("abcbc","ahdjdjd","djjdjdd"));
data.setRestaurants(Arrays.asList("fjjfjf","kfkfkfk","fjfjjfjf"));
jsonReponse.setData(data);
Gson gson = new Gson();
System.out.println(gson.toJson(jsonReponse));

parse json object using gson in android

i have an json object like this and i am getting this response in my Fragment.
json
{
"data":{
"categories":[
{
"id":"d5c4eedf-093e-422f-8335-6c6376ca3ccb",
"schedule_m_id":1,
"title_en":"Bakery Products",
"title_fr":"Produits de boulangerie",
"subtitle_en":"Bread, Cakes, Cookies, Crackers, Pies",
"subtitle_fr":"Pain, gateaux, biscuits, craquelins, tartes",
"created_at":"2015-03-04 15:39:44",
"updated_at":"2015-03-04 15:39:44"
},
{
"id":"6d1d4945-9910-40ae-82a8-3fe4137c24c2",
"schedule_m_id":2,
"title_en":"Beverages",
"title_fr":"Boissons",
"subtitle_en":"Soft Drinks, Coffee, Tea, Cocoa",
"subtitle_fr":"Boissons gazeuses, café, thé, cacao",
"created_at":"2015-03-04 15:39:44",
"updated_at":"2015-03-04 15:39:44"
}
]
},
"result":"success"
}
and my categories class is like this:
public class Categories {
private int id;
private String title_en;
private String title_fr;
private int schedule_m_id;
private String subtitle_en;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle_en() {
return title_en;
}
public void setTitle_en(String title_en) {
this.title_en = title_en;
}
public String getTitle_fr() {
return title_fr;
}
public void setTitle_fr(String title_fr) {
this.title_fr = title_fr;
}
public int getSchedule_m_id() {
return schedule_m_id;
}
public void setSchedule_m_id(int schedule_m_id) {
this.schedule_m_id = schedule_m_id;
}
public String getSubtitle_en() {
return subtitle_en;
}
public void setSubtitle_en(String subtitle_en) {
this.subtitle_en = subtitle_en;
}
}
In my fragment how can i parse this json object. i need to make an ArrayList which type is "Categories". i need this Categories object List to make an custom adapter. Can anybode help me.
JSONObject jsonObject = (JSONObject) response;
JSONObject dataProject = jsonObject.getJSONObject("data");
JSONArray products = dataProject.getJSONArray("categories");
Gson gson = new Gson();
Categories categories = new Categories();
ArrayList<Categories> items = new ArrayList<Categories>();
int productCount = products.length();
for (int i = 0; i < productCount; i++) {
categories = gson.fromJson(products.get(i), Categories.class);
items.add(categories);
}
```
I posting a class working with gson volley May be Helpful for you....
Step1. For Parsing your json data use "www.jsonschema2pojo.org/" and generate pojo classes. copy classes in your project with same name.
Step2. Just create a GsonRequest Class as follows (taken from https://developer.android.com/training/volley/request-custom.html)
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param clazz Relevant class object, for Gson's reflection
* #param headers Map of request headers
*/
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
Step3.Now in your main Activity just use this "GsonRequest" class like that:
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
GsonRequest<MyPojoClass> gsonRequest = new GsonRequest<MyPojoClass>(
Request.Method.GET,
apiurl,
MyPojoClass.class,
mySuccessListener(),
myErrorListener());
//Add below these code lines for "Retry" data fetching from api
gsonRequest.setRetryPolicy(new DefaultRetryPolicy(
5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(gsonRequest);
}
private Response.Listener<MyPojoClass> mySuccessListener() {
return new Response.Listener<CustomRequest>() {
#Override
public void onResponse(MyPojoClass pRequest) {
//do something
}
};
}
private Response.ErrorListener myErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println(volleyError.getMessage().toString());
}
};
}

How to deserialize JSON object from generic type in Java?

I am trying to map JSON response as below:
{
object: {
id: 1
name: "my name"
email: "username#mail.com"
username: "username"
password: "password"
mobile: "##########"
fbAccessToken: "----------"
img: null
}
errorMessage: ""
successMessage: ""
technicalErrorMessage: ""
error: false
}
so I wrote this method:
private <T> ResponseEntity<T> processedRequest(HttpRequestBase requestBase, Class<T> tClass) throws IOException {
HttpResponse response = httpClient.execute(requestBase);
HttpEntity entity = response.getEntity();
Reader reader = new InputStreamReader(entity.getContent());
Type type = new TypeToken<ResponseEntity<T>>() {}.getType();
ResponseEntity<T> responseEntity = gson.fromJson(reader, type);
return responseEntity;
}
based on ResponseEntity class:
public class ResponseEntity<T> {
private T object;
private boolean isError;
private String errorMessage;
private String successMessage;
private String technicalErrorMessage;
public ResponseEntity() {
setSuccessMessage("");
setError(false);
setErrorMessage("");
setTechnicalErrorMessage("");
}
public T getObject() {
return object;
}
public void setObject(T object) {
this.object = object;
}
public boolean isError() {
return isError;
}
public void setError(boolean error) {
this.isError = error;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getTechnicalErrorMessage() {
return technicalErrorMessage;
}
public void setTechnicalErrorMessage(String technicalErrorMessage) {
this.technicalErrorMessage = technicalErrorMessage;
}
public String getSuccessMessage() {
return successMessage;
}
public void setSuccessMessage(String successMessage) {
this.successMessage = successMessage;
}}
but I am getting result as ResponseEntity<LinkedTreeMap> and the object is map of (Key->Value) not the actual mapped object that send by the Type ResponseEntity<T>.
The image below is what appeared in the debugger:
How GSON should understand, which class it should use in place of T and what is the "actual mapped object that send by the Type" to fill it's fields? We have type erasure for generics in Java, so no way in runtime to understand what it T. No way, so gson just uses generic Map<String, String>().
Take a look at responses to this question, it's the same situation.

Categories