Dynamic Response from Retrofit Json to Gson Android Kotlin/Java - java

I have dynamic json format from rest API like this :
{
"data": {
"response_code": "success",
"value": {
"Table": [
{
"id": 5,
"username": "blahblah",
"password": "blahblah",
"role": 2,
"email": "blah#tes.com",
"tanggal_buat": "2019-01-01T00:00:00"
}
]
}
},
"meta": {
"http_status": 200
}
}
Object "value" has an object array name "Table". Table can contain value from my database dynamically depend on my query. So, Sometimes the json format will change for example :
{
"data": {
"response_code": "success",
"value": {
"Table": [
{
"id_product": 44,
"product": "blahblah",
"lot": "blahblah",
"qty": 2,
}
]
}
},
"meta": {
"http_status": 200
}
}
How to accept the json value and assign to gson directly with different subclass of "Table"
I try it in retrofit and using kotlin
override fun onResponse(call: Call<MainResp>, response: Response<MainResp>) {
mainResponse : MainResp = response.body()
}

Assuming that you have the following class among others (using sth like http://www.jsonschema2pojo.org/):
class Value {
List<Table> tables;
}
The "Table" class here cannot be completely random!
You'll need to define the possible types of "Table" e.g. Table1, Table2... TableN.
Now you can update Value class with a generic type T instead of Table and write your custom type adapter:
class Value {
List<T> tables;
}
One of the tutorials on how to write your own type adapter is here.

Related

How to include the full object instead of "contained" in HAPI FHIR

I am very new to hapi FHIR, I am trying to encode the request in following format.
CoverageEligibilityRequest coverageEligibilityRequest = new CoverageEligibilityRequest();
Patient patient = new Patient().addIdentifier(new Identifier().setType(getPatientIdentifierCodeableConcept()).setSystem("http://www.abc.xyz").setValue("123"));
coverageEligibilityRequest.setPatient(new Reference(patient));
Above code is java snippet for populating the patient in CoverageEligibilityRequest.
{
"resourceType": "Bundle",
"type": "batch",
"entry": [ {
"resource": {
"resourceType": "CoverageEligibilityRequest",
"id": "7890",
"contained": [ {
"resourceType": "Patient",
"id": "1",
"identifier": [ {
"type": {
"coding": [ {
...
...
}
But I want the request should be of following format
{
"resourceType": "Bundle",
"type": "batch",
"entry": [ {
"resource": {
"resourceType": "CoverageEligibilityRequest",
"id": "7890",
"patient": {
"type": "Patient",
"identifier": {
"type": {
"coding": [ {
...
...
} ]
},
where I want to omit contained with actual string?
FHIR doesn't generally let you express an entire graph of objects as a single resource, so if you're trying to send a Patient resource as part of a CoverageEligibilityRequest resource, the only way you can do that is by setting the patient in the contained field. The CoverageEligibilityResource.patient field is defined as a Reference type and so can only contain the data allowed by a Reference data type and not arbitrary data.
It seems like what you actually want to do is to add a Patient to the HAPI FHIR server and a CoverageEligibilityRequest resource that references the patient. The right way to do this in FHIR is to construct a single batch or transaction bundle containing both of the resources. Basically, you want to construct a Bundle that looks something like this:
{
"resourceType": "Bundle",
"type": "batch",
"entry": [ {
"resource": {
"resourceType": "Patient",
"id": "1",
"identifier": [ {
"type": {
"coding": [ {
...
}
}, {
"resource": {
"resourceType": "CoverageEligibilityRequest",
"id": "7890",
"patient": "Patient/1",
...
The easiest way to construct something similar in HAPI FHIR would be to use a transaction bundle like this:
IGenericClient client = ...
CoverageEligibilityRequest coverageEligibilityRequest = new CoverageEligibilityRequest();
Patient patient = new Patient().addIdentifier(new Identifier().setType(getPatientIdentifierCodeableConcept()).setSystem("http://www.abc.xyz").setValue("123"));
coverageEligibilityRequest.setPatient(new Reference(patient));
client.transaction().withResources(patient, coverageEligibilityRequest);

How to implement Post API JSON with Spring?

I'm having difficulty implementing a JSON to send as a POST call in Spring.
Which is the fastest and most effective way to turn this json into a java object or a map and make the call?
below is an example of a json to send:
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "edge-ws"
},
"spec": {
"selector": {
"matchLabels": {
"run": "edge-ws"
}
},
"replicas": 1,
"template": {
"metadata": {
"labels": {
"run": "edge-ws"
}
},
"spec": {
"containers": [
{
"name": "edge-ws",
"image": "server-tim:latest",
"imagePullPolicy": "Never",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}
this and the second body that has a value (nodeport) that must be taken from a field entered by the user front end side.(page created in html)
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "edge-ws",
"labels": {
"run": "edge-ws"
}
},
"spec": {
"type": "NodePort",
"ports": [
{
"port": 8080,
"targetPort": 80,
"nodePort": 30200,
"protocol": "TCP",
"name": "http"
}
],
"selector": {
"run": "edge-ws"
}
}
}
Both files must be sent with a single click on a button on the front end side.the first call with the first body starts and if everything is ok the second body starts
What should the class that maps objects look like? What should the controller look like instead?
They also gave me an address to call that can only be used on the machine, how can I test this call locally?
Thanks in advance!
You can use google's Gson library to convert the JsonString to Object and then use below code:
Gson gson = new Gson();
Object requestObject = gson.fromJson(jsonString, Object.class);
ResponseObject responseObject = restTemplate.postForObject(url, requestObject, ResponseObject.class);

Generic solution to GSON json object Array with a key

I am trying to find a generic solution in GSON for my project. This JSON has been problematic for me...
I have a class System
public class System{
String systemid;
String systemname;
//getter and setter
}
Rest service sends data in one of two below format, now for the second format I am handling it in a generic way as shown in last, can someone please help me to handle both the formats in a generic way in one piece of code, I am stuck on this from past two days now...
[
{
"atypes": [
{
"systemid": "123",
"systemname": "abc"
},
{
"systemid": "456",
"systemname": "def"
},
{
"systemid": "789",
"systemname": "ghi"
},
{
"id": "0123",
"name": "klm"
},
{
"systemid": "4567",
"systemname": "nop"
}
]
}
]
Or the second format
[
{
"systemid": "123",
"systemname": "abc"
},
{
"systemid": "456",
"systemname": "def"
},
{
"systemid": "789",
"systemname": "ghi"
},
{
"id": "0123",
"name": "klm"
},
{
"systemid": "4567",
"systemname": "nop"
}
]
Now I am handling the last JSON array in the below method, I want to handle both the code in one piece of generic code.
String data = client.executeCommand(Command.GET, new GenericUrl(URL), null);
System[] tList1 = JsonUtil.jsonArrayToObjectArray(data, System[].class);
which calls a generic piece of code
public static <T> T[] jsonArrayToObjectArray(String data, Class<T[]> tClass) throws Exception {
return new Gson().fromJson(data, tClass);
}
Please if someone can help me...
Edit:
This is different from identifying Json object and json array as here both are json array.

how to access jsonarray using retrofit

how to access districtID(key) value and name(key) value using retrofit ?
this is json file...
{
"error": false,
"message": "District successfully fetched.",
"districts": [
{
"districtID": "DIS260920181",
"name": "Raipur"
},
{
"districtID": "DIS260920182",
"name": "Bilaspur"
},
{
"districtID": "DIS011020186",
"name": "korba"
},
{
"districtID": "DIS011020187",
"name": "jagdalpur"
},
{
"districtID": "DIS021020188",
"name": "surguja"
},
{
"districtID": "DIS021020189",
"name": "Mungeli"
}
]
}
Please help :(
Convert JSON Array into POJO classes and then call api using retrofit network call.
Use this link
copy your json and it gives you two model class you should put the parent class which contains error , message and districts as your output in your request. something like this
#GET("your_url")
Call<ModelExample> nameOfMethod();
And then you have list of districts. you can access to the items and districtID and name
I hope it helps you :)
for onResponse:
--------------------------
Call<ModalClass> daoCall = api.getconnectdata();
modelclass.enqueue(new Callback<ModalClass>() {
#Override
public void onResponse(Call<ModalClass> call, Response<ModalClass>
response) {
if(response.isSuccessful()){
modelclass= response.body();
List<Districts> list = modelclass.getAddData().getData();
adapter = new CustomListAdapter(getApplicationContext(),list);
listView.setAdapter(adapter);
}
}

Modifying katharsis json response

I am using katharsis with spring boot.
I want to change the 'id' key in json response with my table PK column name, following is the katharsis response
{
"type": "table-name",
"id": "A",
"attributes": {
"description": "AAA"
},
"relationships": {
},
"links": {
"self": "http://localhost/table-name/A"
}
}
I want to change the above "id": "A" with "coulmnName":"A".
Answer on katharsis github repository
https://github.com/katharsis-project/katharsis-core/issues/203

Categories

Resources