Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio中正确解析JSON?_Java_Android_Json_Android Studio - Fatal编程技术网

Java 在Android Studio中正确解析JSON?

Java 在Android Studio中正确解析JSON?,java,android,json,android-studio,Java,Android,Json,Android Studio,好的,对Android Studio来说是全新的,但我一直在尝试在Android Studio中解析backpack.tf的json,我有点卡住了 下面是我将尝试解析的json的一个小片段: { "response": { "success": 1, "current_time": 1448658000, "items": { "A Color Similar to Slate": { "last_updated": 1448654

好的,对Android Studio来说是全新的,但我一直在尝试在Android Studio中解析backpack.tf的json,我有点卡住了

下面是我将尝试解析的json的一个小片段:

{
"response": {
    "success": 1,
    "current_time": 1448658000,
    "items": {
        "A Color Similar to Slate": {
            "last_updated": 1448654419,
            "quantity": 48,
            "value": 99
        },
我用来解析JSON的代码如下:

   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;
非常感谢

1)创建一些(自定义,根据您的需要调整)
Model
class

public class Model {
    private String title;
    private List<String> authors;
//getters fields, magic ...
}

1) 用JSON解析器解析它

BufferedReader br = new BufferedReader(new FileReader(JSON_PATH));
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(br).getAsJsonObject();
两种方法都需要GSON库

请尝试以下方法:

您有JSON:

   {"response":{
        "success": 1,
                "current_time": 1448658000,
                "items": {
            "A Color Similar to Slate": {
                "last_updated": 1448654419,
                        "quantity": 48,
                        "value": 99
            },
        }
    }
    }
代码:


允许使用Gson库吗?您可以使用Gson或jackson之类的util-lib。我从来没有听说过Gson!是否有一个很好的教程可供查阅?可能是github上的文档;)看看我的回答你被困在哪里了?请说得更具体些。这并没有回答他具体的问题。他不妨去看看其他GSON的例子。我也喜欢GSON,但我讨厌它自动成为任何JSON解析问题的答案。第三方库并不总是正确的答案。我已经提出了一个解决方案。也许不是最好的,但它解决了OP问题。如果有另一个解决方案,我会给它+1,如果它也能工作,如果它合适我不确定我是否要实现这个,我已经建立了一个JSONTask类,为了获取JSON文件,我只需要解析它。你能用它作为模型吗?
   {"response":{
        "success": 1,
                "current_time": 1448658000,
                "items": {
            "A Color Similar to Slate": {
                "last_updated": 1448654419,
                        "quantity": 48,
                        "value": 99
            },
        }
    }
    }
  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();
    }