【问题标题】:GSON how to parse array with dynamic typeGSON如何解析动态类型的数组
【发布时间】:2012-11-13 16:11:36
【问题描述】:

查看这个 Json 文件,KEY 始终是一个字符串,但 VALUE 有时是一个字符串,但有时是具有两个字符串字段的类型化对象。

如何使用 GSON 解析它?

{
    "property": [
        {
            "key": "key_A",
            "value": "value_A"
        },
        {
            "key": "key_B",
            "value": "value_B"
        },
        {
            "key": "key_C",
            "value": {
                "param_C_1": "value_C_1", 
                "param_C_2": "value_C_2"

            }
        }
    ]
}

【问题讨论】:

    标签: java json parsing types gson


    【解决方案1】:

    第一件事就是把这个json文件解析成java就可以了 这样:-

     try {
                    InputStream is;
                    //read the whole json file stored in assets
    //below is android way of opening file from local resource folder, you can use other means to open
                    is = getApplicationContext().getAssets().open("jsonfile.json");
                    int size = is.available();
    
                    byte[] buffer = new byte[size];
    
                    is.read(buffer);
    
                    is.close();
    
                    //convert the json file to string
                    String bufferString = new String(buffer);
    
                    JSONObject jsonObject;
                    JSONArray jsonArray;
                    jsonObject = new JSONObject(bufferString);
                    jsonArray=jsonObject.getJSONArray("property");
                    for (int i=0;i<jsonArray.length();i++){
                        jsonObject = jsonArray.getJSONObject(i);
                        JSONObject s = jsonArray.optJSONObject(i);
                        String s2 = s.getString("value");
                        if(s2.contains("{")){
                            JSONObject jobject = new JSONObject(s2);
                            String valueUnderValue1 = jobject.getString("param_C_1");
                            String valueUnderValue2 = jobject.getString("param_C_2");
                        }
                    }
    
    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                            } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
    
    
                }
    

    然后创建一个类,该类将包含您从 json 文件中获得的所有值。 假设该类是 MyClass,包含您从 json 文件中获得的所有值。

    创建 MyClass 对象,然后

    MyClass obj = new MyClass();
    Gson gson = new Gson();
    JSONObject onj = new JSONObject();
            JSONArray userDataValues = new JSONArray();
    //again convert to json
    userDataValues.put(new JSONObject(gson.toJson(obj)));
    //serialized the object
    onj.put("property", userDataValues);
    

    我希望这是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      相关资源
      最近更新 更多