简体   繁体   English

未标记所有内容时,如何使用 Gson 解析 Json 数据

[英]How do I parse Json data with Gson when not everything is labelled

I have happily been using Google Gson to parse extract some JSON metadata of the form我一直很高兴地使用 Google Gson来解析提取表单的一些 JSON 元数据

{
  "lowlevel": {
    "average_loudness": 0.570070445538
  },
  "rhythm": {
     "beats_count": 502, 
     "bpm": 128.347702026
  }, 
  "tonal": {
     "chords_changes_rate": 0.0534749031067
     "tuning_diatonic_strength": 0.431238204241, 
     "tuning_equal_tempered_deviation": 0.164615109563, 
     "tuning_frequency": 434.193115234, 
     "tuning_nontempered_energy_ratio": 0.847496032715
  }
}

Using this使用这个

public class AcousticBrainzLowlevelWrapper
{
    private AcousticBrainzLowLevelRhythm rhythm;
    private AcousticBrainzLowLevelTonal tonal;

    public AcousticBrainzLowLevelRhythm getRhythm()
    {
        return rhythm;
    }

    public void setRhythm(AcousticBrainzLowLevelRhythm rhythm)
    {
        this.rhythm = rhythm;
    }

    public AcousticBrainzLowLevelTonal getTonal()
    {
        return tonal;
    }

    public void setTonal(AcousticBrainzLowLevelTonal tonal)
    {
        this.tonal = tonal;
    }
}

and

 AcousticBrainzLowlevelWrapper low = gson.fromJson(result, AcousticBrainzLowlevelWrapper.class) ;

(Full JSON can be seen here ) (完整的 JSON 可以在这里看到)

but now the API has been extended to allow multiple lookups such as this url但现在 API 已经扩展到允许多次查找,比如这个url

which now returns现在返回

{
  "96685213-a25c-4678-9a13-abd9ec81cf35": {
    "0": {
      "lowlevel": {
        "average_loudness": 0.570070445538
      },
      "rhythm": {
        "beats_count": 502, 
        "bpm": 128.347702026
      }, 
      "tonal": {
        "chords_changes_rate": 0.0534749031067
        "tuning_diatonic_strength": 0.431238204241, 
        "tuning_equal_tempered_deviation": 0.164615109563, 
        "tuning_frequency": 434.193115234, 
        "tuning_nontempered_energy_ratio": 0.847496032715
     }
  }
  .....
  "78787888-a25c-4678-9a13-abd9ec81cf35": {
    "0": {
      "lowlevel": {
      ......
  ..

The difference being that the json doesn't define what "96685213-a25c-4678-9a13-abd9ec81cf35" and "78787888-a25c-4678-9a13-abd9ec81cf35" are, or what "0" is.不同之处在于 json 没有定义“96685213-a25c-4678-9a13-abd9ec81cf35”和“78787888-a25c-4678-9a13-abd9ec81cf35”是什么,或者“0”是什么。

So I know what they represent (MusicBrainzRecording and offset) but I cannot create a class like AcousticBrainzLowlevelWrapper to represent this, so how do I parse this new api.所以我知道它们代表什么(MusicBrainzRecording 和 offset),但我无法创建像AcousticBrainzLowlevelWrapper这样的类来表示这一点,那么我该如何解析这个新的 api。

Update I tried creating更新我尝试创建

  public class AcousticBrainzLowLevelList
{
    private Map<String, AcousticBrainzLowlevelWrapper> data = new HashMap<>();

    public Map<String, AcousticBrainzLowlevelWrapper> getData()
    {
        return data;
    }

    public void setData(Map<String, AcousticBrainzLowlevelWrapper> data)
    {
        this.data = data;
    }
}

and then calling然后打电话

 AcousticBrainzLowLevelList lowMap = gson.fromJson(result, AcousticBrainzLowLevelList.class) ;

but nothing get added to the map.但没有任何内容添加到地图中。 Unsuprisingly because data I dont' see how can i give a name since there is no consistent name at the top level.不出所料,因为data我不知道如何命名,因为顶层没有一致的名称。

It seems to me that your input JSON could be parsed to produce a Java class of type Map<String,Map<Integer,AcousticBrainzLowlevelWrapper>> :在我看来,您的输入 JSON 可以被解析为生成Map<String,Map<Integer,AcousticBrainzLowlevelWrapper>>类型的 Java 类:

Type type = new TypeToken<Map<String,Map<Integer,AcousticBrainzLowlevelWrapper>>>(){}.getType();
Map<String,Map<Integer,AcousticBrainzLowlevelWrapper>> result = gson.fromJson(json, type);

As I wrote it, I might as well post it: Similar to Maurice's answer正如我写的那样,我不妨发布一下:类似于莫里斯的回答

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

public class Main {

    private final static String jsonSingle = 
        "{ \"attribute1\": \"value1\",  \"attribute2\": \"value2\" }";

    private final static String jsonMultiple = 
        "{\n" +
        "  \"96685213-a25c-4678-9a13-abd9ec81cf35\": {\n" +
        "    \"0\": { \"attribute1\": \"value1\", \"attribute2\": \"value2\" }\n" +
        "  },\n" +
        "  \"78787888-a25c-4678-9a13-abd9ec81cf35\": {\n" +
        "    \"0\": { \"attribute1\": \"value3\",  \"attribute2\": \"value4\" }\n" +
        "}}";

    public static void main(String[] args) {
        MyBean bean = new Gson().fromJson(jsonSingle, MyBean.class);
        System.out.println(bean);

        Type type = new TypeToken<Map<String, Map<String, MyBean>>>(){}.getType();
        Map<String, String> myMap = new Gson().fromJson(jsonMultiple, type);
        System.out.println(myMap);
    }   
}

MyBean class: MyBean 类:

class MyBean {
    String attribute1;
    String attribute2;

    public String getAttribute1() {
        return attribute1;
    }

    public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
    }

    public String getAttribute2() {
        return attribute2;
    }

    public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
    }

    @Override
    public String toString() {
        return "MyBean: <attribute1: " + attribute1 + " | " + "attribute2: " + attribute2 + ">";
    }
}

Outputs:输出:

MyBean: <attribute1: value1 | attribute2: value2>

and

{96685213-a25c-4678-9a13-abd9ec81cf35={0=MyBean: <attribute1: value1 | attribute2: value2>}, 78787888-a25c-4678-9a13-abd9ec81cf35={0=MyBean: <attribute1: value3 | attribute2: value4>}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM