Alright, totally new to Android Studio, but I've been trying to parse backpack.tf's json in Android Studio, and I'm a bit stuck.
Here is a little snippet of json I would try to parse:
{
"response": {
"success": 1,
"current_time": 1448658000,
"items": {
"A Color Similar to Slate": {
"last_updated": 1448654419,
"quantity": 48,
"value": 99
},
And the code I'm using to parse JSON is here:
String finalJSON = buffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
JSONArray parentArray = parentObject.getJSONArray("A Color Similar to Slate");
JSONObject finalObject = parentArray.getJSONObject(3);
int price = finalObject.getInt("value");
return "$" + price;
Thanks a bunch!
Try this:
You have JSON:
{"response":{
"success": 1,
"current_time": 1448658000,
"items": {
"A Color Similar to Slate": {
"last_updated": 1448654419,
"quantity": 48,
"value": 99
},
}
}
}
Code:
String finalJSON =buffer.toString();;
JSONObject parentObject = null;
try {
parentObject = new JSONObject(finalJSON);
JSONObject objectA_Color=parentObject.getJSONObject("response").getJSONObject("items").getJSONObject("A Color Similar to Slate");
int value=objectA_Color.getInt("value");
} catch (JSONException e) {
e.printStackTrace();
}
1) Create some (custom, adjusted to your needs) Model
class
public class Model {
private String title;
private List<String> authors;
//getters fields, magic ...
}
2) Parse your JSON (
public static final String JSON_PATH = "/Users/dawid/Workspace/Test/test.json";
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader(JSON_PATH));
Model model = gson.fromJson(br, Model.class);
OR
1) Parse it with JSON Parser
BufferedReader br = new BufferedReader(new FileReader(JSON_PATH));
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(br).getAsJsonObject();
Both ways Require GSON library https://github.com/google/gson
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.