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

时间:2018-08-21 12:49:21

标签: java json gson

我一直很高兴使用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
  }
}

使用

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;
    }
}

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

(可以看到完整的JSON here

但是现在,API已扩展为允许多次查找,例如url

现在返回

{
  "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": {
      ......
  ..

区别在于json并未定义什么是“ 96685213-a25c-4678-9a13-abd9ec81cf35”和“ 78787888-a25c-4678-9a13-abd9ec81cf35”或“ 0”是什么。

所以我知道它们代表什么(MusicBrainzRecording和offset),但是我无法创建类似AcousticBrainzLowlevelWrapper的类来表示它,所以我该如何解析这个新的api。

更新 我尝试创建

  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;
    }
}

然后致电

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

,但是什么都没有添加到地图上。毫无疑问,因为data我不知道如何命名,因为在顶层没有一致的名称。

2 个答案:

答案 0 :(得分:1)

在我看来,您的输入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);

答案 1 :(得分:0)

在我写它的时候,我不妨张贴它: 类似于莫里斯的答案

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 {
    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 + ">";
    }
}

输出:

MyBean: <attribute1: value1 | attribute2: value2>

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