You should use the Gson library as a json parser .
add this dependency to the gradle application file:
compile 'com.google.code.gson:gson:2.8.1'
raw res. json ( ). , json my_json.json
{
"list": [
{
"name": "Faraz Khonsari",
"age": 24
},
{
"name": "John Snow",
"age": 28
},
{
"name": "Alex Kindman",
"age": 29
}
]
}
:
public class MyModel {
@SerializedName("list")
public ArrayList<MyObject> list;
static public class MyObject {
@SerializedName("name")
public String name;
@SerializedName("age")
public int age;
}
}
json :
public String inputStreamToString(InputStream inputStream) {
try {
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes, 0, bytes.length);
String json = new String(bytes);
return json;
} catch (IOException e) {
return null;
}
}
json :
String myJson=inputStreamToString(mActivity.getResources().openRawResource(R.raw.my_json));
json :
MyModel myModel = new Gson().fromJson(myJson, MyModel.class);
Json ! !