Reputation: 713
I am retrieving an Json from an API. The problem is that I don't have any idea on how to convert Json format to a format maybe a java class so that it can be used by me to get the intended values.
This is my sample Json format
{
"par_a":".....",
"par_b": [ {
"b1":".....",
"b2":".....",
"b3":".....",
"b4":".....",
"b5":".....",
"b6":".....",
"b7":".....",
"b8":".....",
"b9":".....",
"b10": { "b10-1":".....", "b10-2":".....", "b10-3":"....." } ,
"b11": { "b11-1": ["....." ], "b11-2": ["....." ] } ,
"b13":"......."
} ]
}
I do not have an option to contact the owner of the API and understand how this works and hence any help would be really grateful.
I have seen many posts online but I have nowhere seen a Json format as mine hence no solution has worked for me.
Upvotes: 1
Views: 10086
Reputation: 452
Enter obtained JSON in the window and press generate. This will make your class Gson with all attributes in Json. Create getter and setter for attributes. Result will be like
public class DataRetriever {
@Expose
@SerializedName("par_b")
private List<Par_bEntity> mPar_b;
@Expose
@SerializedName("par_a")
private String mPar_a;
public List<Par_bEntity> getmPar_b() {
return mPar_b;
}
public void setmPar_b(List<Par_bEntity> mPar_b) {
this.mPar_b = mPar_b;
}
public String getmPar_a() {
return mPar_a;
}
public void setmPar_a(String mPar_a) {
this.mPar_a = mPar_a;
}
/**
* Similar to these getter and setter, you can add getter and setter for sub
classes also
*/
}
class Par_bEntity {
@Expose
@SerializedName("b13")
private String mB13;
@Expose
@SerializedName("b11")
private B11Entity mB11;
@Expose
@SerializedName("b10")
private B10Entity mB10;
@Expose
@SerializedName("b9")
private String mB9;
@Expose
@SerializedName("b8")
private String mB8;
@Expose
@SerializedName("b7")
private String mB7;
@Expose
@SerializedName("b6")
private String mB6;
@Expose
@SerializedName("b5")
private String mB5;
@Expose
@SerializedName("b4")
private String mB4;
@Expose
@SerializedName("b3")
private String mB3;
@Expose
@SerializedName("b2")
private String mB2;
@Expose
@SerializedName("b1")
private String mB1;
}
class B10Entity {
@Expose
@SerializedName("b10-3")
private String b10_3;
@Expose
@SerializedName("b10-2")
private String b10_2;
@Expose
@SerializedName("b10-1")
private String b10_1;
}
class B11Entity {
@Expose
@SerializedName("b11-2")
private List<String> b11_2;
@Expose
@SerializedName("b11-1")
private List<String> b11_1;
}
At the place where you obtain json string get values to DataRetriever class by calling
DataRetriever mDataRetriever = new Gson().fromJson(jsonString, DataRetriever.class);
Using mDataRetriever object and getter methods you can retrieve value using
Ex : mDataRetriever.getmPar_a()for value “par_a”
Upvotes: 1
Reputation: 691
Use this way
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("filename.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
and then parse the json
JSONObject obj = new JSONObject(loadJSONFromAsset());
Upvotes: 1
Reputation: 87
You have an excellent library doing that, Use GSON lib. You just have to create an object representing your Json File, and when you will decode it, it will feel your object with your json datas !
Upvotes: 1
Reputation: 3391
If you get the data from your json file and it store in your project then your solution is here.
Gson gson = new Gson();
String customRatioResponse = LocalUtils.loadJSONFromAsset(getActivity(), "custom_ratio.json"); // here custom_ratio.json is my json file. it's store in assets folder of project
CustomRatioList getSampleImageResponse = gson.fromJson(customRatioResponse, CustomRatioList.class); // here CustormRatioList is my POJO file
Log.i(TAG, "getAllCategory() -> Offline list size: " + (getSampleImageResponse.getCustomRatio() != null ? getSampleImageResponse.getCustomRatio().size() : 0));
return getSampleImageResponse.getCustomRatio();
Here, If your Json file having too much data or list of data then it gives you arrayList type of data.
LocalUtils.java
public class LocalUtils {
public static String loadJSONFromAsset(Context context, String jsonFilePath) {
String json = null;
try {
InputStream is = context.getAssets().open(jsonFilePath);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (Throwable e) {
e.printStackTrace();
return "";
}
return json;
}
}
If will you get pojo of your json then go to http://www.jsonschema2pojo.org/ paste your sample data json and get pojo structure. Thank you for read.
Upvotes: 1
Reputation: 136
A nice workaround would be to create a Model corresponding to the response you're receiving from the server and using GSON library you can easily deserialize the json to your model. Your model will be something like this:
public class MyResponseModel {
@SerializedName("par_a")
private String parameterA;
@SerializedName("par_b")
private List<AnotherModel> parametersB;
/.../
}
Upvotes: 1
Reputation: 1619
Suppose you are trying to find the string value for b10-1
:
The code will be like below:
String json = "{par_a:.....,par_b: [ {b1:.....,b2:.....,b3:.....,b4:.....,b5:.....,b6:.....,b7:.....,b8:.....,b9:.....,b10: { b10-1:....., b10-2:....., b10-3:..... } ,b11: { b11-1: [..... ], b11-2: [..... ] } , b13:.......} ]}";
try{
String result = new JSONObject(json).getJSONArray("par_b").getJSONObject(0).getJSONObject("b_10").getString("b10-1");
}catch(Exception e){
}
Upvotes: -1