Java 读取JSON文件并映射对象
所以我有一个巨大的JSON文件,我需要从中提取数据。JSON格式如下所示:Java 读取JSON文件并映射对象,java,json,Java,Json,所以我有一个巨大的JSON文件,我需要从中提取数据。JSON格式如下所示: { "enabled":true, "contentMetadataPartial": { "customTags": { "tag1":"value1" } }, "simulatedChanges": [ 3000, 2500, 400 ], "simulatedUpdateMetadata": [
{
"enabled":true,
"contentMetadataPartial":
{
"customTags":
{
"tag1":"value1"
}
},
"simulatedChanges":
[
3000,
2500,
400
],
"simulatedUpdateMetadata":
[
{
"customTags":
{
"tag1":"value1",
},
"assetName":"asset1234",
},
{
"duration":1111,
"encodedRate":3333,
}
]
}
为了阅读它,我试图创建一个类来映射键和对象。类似于这个问题:
public class ConfigData
{
private Boolean enabled;
private class ContentMetadataPartial
{
private class CustomTags
{
String tag1;
}
}
int[] simulatedChanges = new int[3];
//problem here
}
但是我被数组卡住了,它包含更多的对象,而不仅仅是简单的基本数据类型
JSON文件非常庞大,上面有类似类型的项。我是个新手,可能犯了一些错误。任何对正确方向的帮助都将不胜感激。谢谢 您需要一个类结构,而不是一个完整的类结构: 配置数据:
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
+ "]";
}
}
自定义标签:
package com.example.gson;
public class CustomTags {
private String tag1;
public String getTag1() {
return tag1;
}
public void setTag1(String tag1) {
this.tag1 = tag1;
}
}
模拟更新元数据:
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;
}
}
格桑曼:
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
}
]
}
我用它来生成类。package com.webom.practice;
package com.webom.practice;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Sample {
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
public static void main(String[]args) {
String Jsondata="{\r\n" +
" \"enabled\":true,\r\n" +
" \"contentMetadataPartial\":\r\n" +
" {\r\n" +
" \"customTags\":\r\n" +
" {\r\n" +
" \"tag1\":\"value1\"\r\n" +
" }\r\n" +
" },\r\n" +
" \"simulatedChanges\":\r\n" +
" [\r\n" +
" 3000,\r\n" +
" 2500,\r\n" +
" 400\r\n" +
" ],\r\n" +
" \"simulatedUpdateMetadata\":\r\n" +
" [\r\n" +
" {\r\n" +
" \"customTags\":\r\n" +
" {\r\n" +
" \"tag1\":\"value1\",\r\n" +
" },\r\n" +
" \"assetName\":\"asset1234\",\r\n" +
" },\r\n" +
" {\r\n" +
" \"duration\":1111,\r\n" +
" \"encodedRate\":3333,\r\n" +
" }\r\n" +
" ]\r\n" +
"}\r\n" ;
JSONObject json = new JSONObject(Jsondata);
System.out.println(json);
Map<String,Object> dataConversion=Sample.jsonToMap(json);
System.out.println(dataConversion);
}
}
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类样本{
公共静态映射jsonToMap(JSONObject json)抛出JSONException{
Map retMap=newhashmap();
if(json!=JSONObject.NULL){
retMap=toMap(json);
}
返回地图;
}
公共静态映射toMap(JSONObject对象)抛出JSONException{
Map Map=newhashmap();
迭代器keysItr=object.keys();
while(keysItr.hasNext()){
String key=keysItr.next();
对象值=Object.get(键);
if(JSONArray的值实例){
值=toList((JSONArray)值);
}
else if(JSONObject的值实例){
值=toMap((JSONObject)值);
}
map.put(键、值);
}
返回图;
}
公共静态列表toList(JSONArray数组)抛出JSONException{
列表=新的ArrayList();
对于(int i=0;i
您使用哪个库来解析文件?我计划使用GSON。该数组可以像您声明的那样使用。不需要新的
。对于您称之为“更多对象”的其他对象,您需要创建单独的类,并在容器类中将它们声明为成员。这就是我一直在研究如何将内部类实现为数组元素的原因。我现在要试试阿尔贝托的答案。也许以后我会尝试修改它。
package com.webom.practice;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Sample {
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
public static void main(String[]args) {
String Jsondata="{\r\n" +
" \"enabled\":true,\r\n" +
" \"contentMetadataPartial\":\r\n" +
" {\r\n" +
" \"customTags\":\r\n" +
" {\r\n" +
" \"tag1\":\"value1\"\r\n" +
" }\r\n" +
" },\r\n" +
" \"simulatedChanges\":\r\n" +
" [\r\n" +
" 3000,\r\n" +
" 2500,\r\n" +
" 400\r\n" +
" ],\r\n" +
" \"simulatedUpdateMetadata\":\r\n" +
" [\r\n" +
" {\r\n" +
" \"customTags\":\r\n" +
" {\r\n" +
" \"tag1\":\"value1\",\r\n" +
" },\r\n" +
" \"assetName\":\"asset1234\",\r\n" +
" },\r\n" +
" {\r\n" +
" \"duration\":1111,\r\n" +
" \"encodedRate\":3333,\r\n" +
" }\r\n" +
" ]\r\n" +
"}\r\n" ;
JSONObject json = new JSONObject(Jsondata);
System.out.println(json);
Map<String,Object> dataConversion=Sample.jsonToMap(json);
System.out.println(dataConversion);
}
}