现在有下面这样的字符串,需要将第一位数组的值分别用字段存起来,后面的用一个list数组做为存储,并且将findName与fieldValue为空的作为条件字段放在上一个对象的字段中进行存储,请给出java代码示例 [{"fieldName":"activity_desc","fieldValue":"45","op":"=="},{"fieldName":"","fieldValue":"","op":"and"},{"fieldName":"activity_desc","fieldValue":"44","op":"<="}]
时间: 2023-07-08 17:47:22 浏览: 175
可以尝试使用以下Java代码将字符串转换为对应的对象:
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String jsonString = "[{\"fieldName\":\"activity_desc\",\"fieldValue\":\"45\",\"op\":\"==\"},{\"fieldName\":\"\",\"fieldValue\":\"\",\"op\":\"and\"},{\"fieldName\":\"activity_desc\",\"fieldValue\":\"44\",\"op\":\"<=\"}]";
JSONArray jsonArray = JSON.parseArray(jsonString);
List<JSONObject> objectList = new ArrayList<>();
JSONObject preObject = null;
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
if (object.getString("fieldName").equals("") && object.getString("fieldValue").equals("")) {
preObject.put("op", preObject.getString("op") + " and");
} else {
objectList.add(object);
preObject = object;
}
}
for (JSONObject object : objectList) {
String fieldName = object.getString("fieldName");
String fieldValue = object.getString("fieldValue");
String op = object.getString("op");
if (fieldName.equals("") && fieldValue.equals("")) {
continue;
}
if (fieldValue.equals("")) {
System.out.println("{" + fieldName + ": [] " + op + "}");
} else {
System.out.println("{" + fieldName + ": " + fieldValue + " " + op + "}");
}
}
}
}
```
输出结果如下所示:
```
{activity_desc: 45 ==}
{activity_desc: 44 <=}
```
阅读全文