字符串在密钥对上以适当的类型分割为json

时间:2017-02-27 18:18:37

标签: java json string

Audiocheckstring = "Truncation = NONE, Level = NORMAL, RMS = 4178.84, SNR = 48.08, Max = 29333, Min = -30746, DC_Offset = 0, Num_Clipped = 0, LostBuffers=0, Num_Samples = 57600, Duration = 3.60, Speech Start 0 End 0";

我需要的输出是:

   {
    "DC_Offset": -2,
    "Duration": 2.88,
    "Level": "NORMAL",
    "LostBuffers": 0,
    "Max": 26847,
    "Min": -30303,
    "Num_Clipped": 0,
    "Num_Samples": 46080,
    "RMS": 3932.73,
    "SNR": 50.97,
    "Speech": {
        "Start": 0,
        "End": 0
    },
    "Truncation ": "NONE"
  }

最好使用Java8 oneliner,因此我可以在使用with方法分配给com.amazonaws.services.dynamodbv2.document.Item时动态执行此操作。

到目前为止,我有这一行有点工作,但把所有人视为字符串。

  new ObjectMapper()
      .writeValueAsString(Splitter.on( "," )
                 .withKeyValueSeparator( '=' )
                 .split( Audiocheckstring.replaceAll("Speech Start ([0-9]+)\\s+End ([0-9]+)", "Speech Start=$1, Speech End=$2") )
                          )
      )

产生;

  {
    " DC_Offset ": " -2",
    " Duration ": " 2.88",
    " Level ": " NORMAL",
    " LostBuffers": "0",
    " Max ": " 26847",
    " Min ": " -30303",
    " Num_Clipped ": " 0",
    " Num_Samples ": " 46080",
    " RMS ": " 3932.73",
    " SNR ": " 50.97",
    " Speech End": "0",
    " Speech Start": "0",
    "Truncation ": " NONE"
  }

0 个答案:

没有答案