如何解析复杂的对象使用Java 8流?



我有json对象如下,我需要解析并返回逗号分隔值

{
"globalCode" : "12"
"fields" : [
{
"localCode" : "72879",
"fieldValues" : [
{
"value" : null,
"subFields" : [
{
"localCode" : "980"
"subFieldValues" : [
{
"value" :  "lon"
}
]
},
{
"localCode" : "98"
"subFieldValues" : [
{
"value" :  "KPO"
}
]
}

]
}       
]
}
]
}

我想使用java 8流实现以下目标。下面的函数应该返回字符串"朗,KPO"

第2行函数,返回一个字段对象从字段数组我不确定如何使用java 8流添加其他部分。

public String getValue(){
StringBuffer buffer = new StringBuffer();
List<Field> fields  = //get fields for globalCode 72879 // line 2
for(Field field : fields    ){
List<FieldValue> fieldValues : field.getFieldValues();
for(FieldValue fieldValue : fieldValues)
{
if(fieldValue.getValue() != null){
buffer.append(fieldValue.getValue());
buffer.append(",");
}
else{
List<SubField> subFields = values.getSubFields();
for(SubField subField: subFields ){
List<SubFieldValue> subFieldValues : subField.getSubFieldValues();
for(SubFieldValue subFieldValue : subFieldValues  ){
buffer.append(subFieldValue.getValue());
buffer.append(",");
}        
}
}
}
}
return buffer.toString();
}
fields.stream().flatMap(field -> field.getFieldValues().stream()).forEach(fieldValue -> {
if (fieldValue.getValue() != null) {
buffer.append(fieldValue.getValue());
buffer.append(",");
} else {
List<SubField> subFields = fieldValue.getSubFields();
for (SubField subField : subFields) {
List<SubFieldValue> subFieldValues = subField.getSubFieldValues();
for (SubFieldValue subFieldValue : subFieldValues) {
buffer.append(subFieldValue.getValue());
buffer.append(",");
}
}
}
});

如果您还想替换else语句中的for循环:

fields.stream().flatMap(field -> field.getFieldValues().stream()).forEach(fieldValue -> {
if (fieldValue.getValue() != null) {
buffer.append(fieldValue.getValue());
buffer.append(",");
} else {
List<SubField> subFields = fieldValue.getSubFields();
subFields.stream().map(SubField::getSubFieldValues).forEach(subFieldValues -> subFieldValues.forEach(subFieldValue -> {
buffer.append(subFieldValue.getValue());
buffer.append(",");
}));
}
});

你可以这样做:

Stream.concat(
list.stream().flatMap(field -> field.getFieldValues().stream()
.map(FieldValue::getValue)), 
list.stream()
.flatMap(field -> field.getFieldValues().stream())
.flatMap(fieldValue -> fieldValue.getSubFields().stream())
.flatMap(subField -> subField.getSubFieldValues().stream())
.map(SubFieldValue::getValue)
)
.filter(Objects::nonNull)
.collect(Collectors.joining(","));

ComplexObject.java

@Getter
public class ComplexObject
{
private String globalCode;
private List<Fields> fields;
}

Fields.java

@Getter
public class Fields
{
private String localCode;
private List<FieldValues> fieldValues;
}

FieldValues.java

@Getter
public class FieldValues {
private String value;
private List<SubFields> subFields;
}

SubFields.java

@Getter
public class SubFields
{
private String localCode;
private List<SubFieldValues> subFieldValues;
}

SubFieldValues.Java

@Getter
public class SubFieldValues{
private String value;
}

主类

public class Sol {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{n" +
" "globalCode" : "12",n" +
"  "fields" : [n" +
"     {n" +
"           "localCode" : "72879",n" +
"           "fieldValues" : [n" +
"                {n" +
"                      "value" : null,n" +
"                      "subFields" : [n" +
"                           {n" +
"                                 "localCode" : "980",n" +
"                                 "subFieldValues" : [n" +
"                                         {n" +
"                                          "value" :  "lon"n" +
"                                         }n" +
"                                  ]n" +
"                           },n" +
"                            {n" +
"                                 "localCode" : "98",n" +
"                                 "subFieldValues" : [n" +
"                                         {n" +
"                                          "value" :  "KPO"n" +
"                                         }n" +
"                                  ]n" +
"                           }n" +
"                              n" +
"n" +
"                      ]n" +
"                }       n" +
"           ]n" +
"     }n" +
"  ]n" +
"}";
ComplexObject complexObject = objectMapper.readValue(json, ComplexObject.class);

List<Fields> fields = complexObject.getFields();
Stream.concat(
fields.stream().flatMap(field -> field.getFieldValues().stream()
.map(FieldValues::getValue)),
fields.stream()
.flatMap(field -> field.getFieldValues().stream())
.flatMap(fieldValue -> fieldValue.getSubFields().stream())
.flatMap(subField -> subField.getSubFieldValues().stream())
.map(SubFieldValues::getValue)
)
.filter(Objects::nonNull)
.reduce((s, s2) -> s + "," + s2).ifPresent(System.out::println);
}
}

输出:

lon,KPO

注意:

  • JSON无效,您错过了几个逗号。

相关内容

  • 没有找到相关文章

最新更新