所以我已经获得了这个巨大的JSON文件,我需要从中提取数据。 JSON格式如下:
[^\\d_]+
为了阅读它,我试图在地图上创建一个类和键和对象。类似于this问题:
{
"enabled":true,
"contentMetadataPartial":
{
"customTags":
{
"tag1":"value1"
}
},
"simulatedChanges":
[
3000,
2500,
400
],
"simulatedUpdateMetadata":
[
{
"customTags":
{
"tag1":"value1",
},
"assetName":"asset1234",
},
{
"duration":1111,
"encodedRate":3333,
}
]
}
但是我被困在数组中,该数组包含更多对象而不仅仅是简单的基本数据类型。
JSON文件很庞大,整个版本都有类似的项目。我对此很新,可能会犯一些错误。任何对正确方向的帮助都表示赞赏。谢谢!
答案 0 :(得分:0)
您需要一个类结构,而不是一个整体:
ConfigData:
package com.example.gson;
import java.util.List;
public class ConfigData {
private Boolean enabled;
private ContentMetadataPartial contentMetadataPartial;
private List<Integer> simulatedChanges = null;
private List<SimulatedUpdateMetadatum> simulatedUpdateMetadata = null;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public ContentMetadataPartial getContentMetadataPartial() {
return contentMetadataPartial;
}
public void setContentMetadataPartial(ContentMetadataPartial contentMetadataPartial) {
this.contentMetadataPartial = contentMetadataPartial;
}
public List<Integer> getSimulatedChanges() {
return simulatedChanges;
}
public void setSimulatedChanges(List<Integer> simulatedChanges) {
this.simulatedChanges = simulatedChanges;
}
public List<SimulatedUpdateMetadatum> getSimulatedUpdateMetadata() {
return simulatedUpdateMetadata;
}
public void setSimulatedUpdateMetadata(List<SimulatedUpdateMetadatum> simulatedUpdateMetadata) {
this.simulatedUpdateMetadata = simulatedUpdateMetadata;
}
@Override
public String toString() {
return "ConfigData [enabled=" + enabled + ", contentMetadataPartial=" + contentMetadataPartial
+ ", simulatedChanges=" + simulatedChanges + ", simulatedUpdateMetadata=" + simulatedUpdateMetadata
+ "]";
}
}
ContentMetadataPartial:
package com.example.gson;
public class ContentMetadataPartial {
private CustomTags customTags;
public CustomTags getCustomTags() {
return customTags;
}
public void setCustomTags(CustomTags customTags) {
this.customTags = customTags;
}
}
CustomTags:
package com.example.gson;
public class CustomTags {
private String tag1;
public String getTag1() {
return tag1;
}
public void setTag1(String tag1) {
this.tag1 = tag1;
}
}
SimulatedUpdateMetadatum:
package com.example.gson;
public class SimulatedUpdateMetadatum {
private CustomTags customTags;
private String assetName;
private Integer duration;
private Integer encodedRate;
public CustomTags getCustomTags() {
return customTags;
}
public void setCustomTags(CustomTags customTags) {
this.customTags = customTags;
}
public String getAssetName() {
return assetName;
}
public void setAssetName(String assetName) {
this.assetName = assetName;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public Integer getEncodedRate() {
return encodedRate;
}
public void setEncodedRate(Integer encodedRate) {
this.encodedRate = encodedRate;
}
}
GsonMain:
package com.example.gson;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.example.gson.ConfigData;
import com.google.gson.Gson;
public class GsonMain {
private static final String jsonFile = "files/input.json";
public static void main(String[] args) {
String content = readFile(jsonFile);
ConfigData conf = new Gson().fromJson(content, ConfigData.class);
System.out.println(conf);
}
private static String readFile(String filename) {
BufferedReader br = null;
FileReader fr = null;
StringBuilder content = new StringBuilder();
try {
fr = new FileReader(filename);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
content.append(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return content.toString();
}
}
你的Json很好地形成了:
{
"enabled":true,
"contentMetadataPartial":
{
"customTags":
{
"tag1":"value1"
}
},
"simulatedChanges":
[
3000,
2500,
400
],
"simulatedUpdateMetadata":
[
{
"customTags":
{
"tag1":"value1"
},
"assetName":"asset1234"
},
{
"duration":1111,
"encodedRate":3333
}
]
}
我已使用此web生成类。
答案 1 :(得分:0)
foldMap