Seenu69
Seenu69

Reputation: 1054

How to create model class for json parsing?

I need to create the model class for the following type of json:

{
    "AdditinalInfo": [
        {
            "Tag": "ORDER",
            "Value": "[{\"EN_CODE\":\"8901233014951\",\"SKU_CODE\":\"1000003\",\"SKU_DESC\":\"5Star crunchy chocolate 33g\" ,\"QUANTITY\":\"1\",\"UNIT_SELLING_PRICE\":\"18.0\"}]"
        }
    ]
}

Please help how can I create model class for the above json. I need to send the json using the POST method.

Upvotes: 2

Views: 6591

Answers (5)

kkost
kkost

Reputation: 3730

You don't need to create model for sending this Json via Retrofit.

@POST("/save")
    Call<JsonElement> request(@Body RequestBody requestBody);

String resultJson = ... // your json
//parse it to RequestBody type
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), resultJson);
//create adapter
Retrofit restAdapter = new Retrofit.Builder()
        .baseUrl(Constants.ROOT_API_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);

Call<JsonElement> result = service.CreateAccount(requestBody);
result.enqueue(new Callback<JsonElement>() {
    @Override
    public void onResponse(Call<JsonElement> call, retrofit2.Response<JsonElement> response) {
        if(response.isSuccessful()){
            JsonElement jsonElement = response.body();
            JsonObject withResponse = jsonElement.getAsJsonObject();
        }
        else{
            System.out.println(response.message());
        }
    }

    @Override
    public void onFailure(Call<JsonElement> call, Throwable t) {

    }
});

Upvotes: 0

Mohammad irshad sheikh
Mohammad irshad sheikh

Reputation: 888

use

http://www.jsonschema2pojo.org/

delete json which shows its a dummy copy past json to json place

click Preview and then finally download zip

done.

Thanks

Upvotes: 5

Saurabh Kataria
Saurabh Kataria

Reputation: 81

{} braces are for object and [] are Arrays .

For example,

    class ModelClass{ 

    public ArrayList<ADDITIONALINFOCLASS> AdditinalInfo;

    public class ADDITIONALINFOCLASS {
    public String Tag;
    public String Value;
   }
 }

The error that you are getting is for wrong parsing, try this code and see if it works.

Upvotes: -1

lubilis
lubilis

Reputation: 4160

You can generate model from json automatically using online tools like THIS

-----------------------------------com.example.AdditinalInfo.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class AdditinalInfo {

    @SerializedName("Tag")
    @Expose
    public String tag;
    @SerializedName("Value")
    @Expose
    public String value;

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Example {

    @SerializedName("AdditinalInfo")
    @Expose
    public List<AdditinalInfo> additinalInfo = new ArrayList<AdditinalInfo>();
}

Upvotes: 1

Burak Yıldırım
Burak Yıldırım

Reputation: 35

class AdditinalInfo {
    public TagValuesPair[] AdditinalInfo;
}

class TagValuesPair {
    public String Tag;
    public Map<String, String> Value;
}
// getter setter constructors are ommitted for simplicity

Upvotes: 0

Related Questions