我正在尝试解析json,它具有多个嵌套列表(输入和输出),该如何解析呢?
{
"status":"current",
"entry":[{
"item":{
"name":"Task1",
"identifier":"T00001",
"input":[{
"type":{
"text":"InputTask1-1"
},
"valueString":"Weekly"
},
{
"type":{
"text":"InputTask1-2"
},
"valueString":"Daily"
}
],
"output":[{
"type":{
"text":"OutputTask1-1"
},
"valueString":"Daily"
},
{
"type":{
"text":"OutputTask1-2"
},
"valueString":"Weekly"
}
]
}
},
{
"item":{
"name":"Task2",
"identifier":"T00002",
"input":[{
"type":{
"text":"InputTask2-1"
},
"valueString":"Weekly"
},
{
"type":{
"text":"InputTask2-2"
},
"valueString":"Daily"
}
],
"output":[{
"type":{
"text":"OutputTask2-1"
},
"valueString":"Daily"
},
{
"type":{
"text":"OutputTask2-2"
},
"valueString":"Weekly"
}
]
}
}
]
}
寻找这样的分隔输出文件:
"TaskName"|"TaskId"|"InputName"|"InputValue"|"OutputName"|"OutputValue"
Task1|T00001|InputTask1-1|Weekly|outputDummyText|outputDummyValue
Task1|T00001|InputTask1-2|Daily|outputDummyText|outputDummyValue
Task1|T00001|inputDummyText|inputDummyValue|OutputTask1-1|Daily
Task1|T00001|inputDummyText|inputDummyValue|OutputTask1-2|Weekly
Task2|T00002|InputTask2-1|Weekly|outputDummyText|outputDummyValue
Task2|T00002|InputTask2-2|Daily|outputDummyText|outputDummyValue
Task2|T00002|inputDummyText|inputDummyValue|OutputTask2-1|Daily
Task2|T00002|inputDummyText|inputDummyValue|OutputTask2-2|Weekly
答案 0 :(得分:2)
定义模型:
data class MyData(val status: String, val entry: List<Entry.ItemContainer>) {
data class Entry(val itemContainer: List<ItemContainer>) {
data class ItemContainer(val item: Item) {
data class Item(
val name: String,
val identifier: String,
val input: List<TypeContainer>,
val output: List<TypeContainer>
) {
data class TypeContainer(val valueString: String, val type: Type) {
data class Type(val text: String)
}
}
}
}
}
从文件中读取json字符串:
val json = File("data.json").readText()
使用Gson将数据映射到定义的模型:
val myData = Gson().fromJson<MyData>(json, MyData::class.java)
以所需格式打印结果(或将其保存到文件中):
println("\"TaskName\"|\"TaskId\"|\"InputName\"|\"InputValue\"|\"OutputName\"|\"OutputValue\"")
for (itemContainer: MyData.Entry.ItemContainer in myData.entry) {
with(itemContainer.item) {
for (typeContainer in input) {
println("$name|$identifier|${typeContainer.type.text}|${typeContainer.valueString}|outputDummyText|outputDummyValue")
}
for (typeContainer in output) {
println("$name|$identifier|inputDummyText|inputDummyValue|${typeContainer.type.text}|${typeContainer.valueString}")
}
}
}
答案 1 :(得分:0)
JSONObject jsonParent = new JSONObject(response);
JSONArray jsonarray = jsonParent .getJSONArray("entry");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject c = jsonarray.getJSONObject(i);
JSONObject jsonItem = c.getString("item");
JSONArray inputItemArray = jsonItem.getJSONArray("input");
for (int j = 0; j < inputItemArray.length(); j++) {
JSONObject inputItemObj = inputItemArray .getJSONObject(j);
String invalueString = inputItemObj.getSting("valueString ");
}
JSONArray outputItemArray = jsonItem.getJSONArray("output");
for (int k = 0; k < outputItemArray.length(); k++) {
JSONObject outputItemObj = outputItemArray.getJSONObject(K);
String outvalueString = outputItemObj.getSting("valueString ");
}
}