如下json串,根据name的值,提取project_id,比如有name值是test3, 想拿到对应的project_id 37:
[{
"creation_time": "2021-04-02T10:03:31.657Z",
"current_user_role_ids": null,
"cve_allowlist": {
"creation_time": "2021-04-02T10:03:31.660Z",
"id": 34,
"items": [],
"project_id": 34,
"update_time": "2021-04-02T10:03:31.660Z"
},
"metadata": {
"public": "false"
},
"name": "test1",
"owner_id": 34,
"owner_name": "test1",
"project_id": 34,
"repo_count": 1,
"update_time": "2021-04-02T10:03:31.657Z"
}, {
"creation_time": "2021-04-06T01:48:12.097Z",
"current_user_role_ids": null,
"cve_allowlist": {
"creation_time": "2021-04-06T01:48:12.098Z",
"id": 37,
"items": [],
"project_id": 37,
"update_time": "2021-04-06T01:48:12.098Z"
},
"metadata": {
"public": "false"
},
"name": "test3",
"owner_id": 37,
"owner_name": "test3",
"project_id": 37,
"update_time": "2021-04-06T01:48:12.097Z"
}]
import com.alibaba.fastjson.JSON;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Demo {
public static void main(String[] args) {
String str = "[{\n" +
" \"creation_time\": \"2021-04-02T10:03:31.657Z\",\n" +
" \"current_user_role_ids\": null,\n" +
" \"cve_allowlist\": {\n" +
" \"creation_time\": \"2021-04-02T10:03:31.660Z\",\n" +
" \"id\": 34,\n" +
" \"items\": [],\n" +
" \"project_id\": 34,\n" +
" \"update_time\": \"2021-04-02T10:03:31.660Z\"\n" +
" },\n" +
" \"metadata\": {\n" +
" \"public\": \"false\"\n" +
" },\n" +
" \"name\": \"test1\",\n" +
" \"owner_id\": 34,\n" +
" \"owner_name\": \"test1\",\n" +
" \"project_id\": 34,\n" +
" \"repo_count\": 1,\n" +
" \"update_time\": \"2021-04-02T10:03:31.657Z\"\n" +
"}, {\n" +
" \"creation_time\": \"2021-04-06T01:48:12.097Z\",\n" +
" \"current_user_role_ids\": null,\n" +
" \"cve_allowlist\": {\n" +
" \"creation_time\": \"2021-04-06T01:48:12.098Z\",\n" +
" \"id\": 37,\n" +
" \"items\": [],\n" +
" \"project_id\": 37,\n" +
" \"update_time\": \"2021-04-06T01:48:12.098Z\"\n" +
" },\n" +
" \"metadata\": {\n" +
" \"public\": \"false\"\n" +
" },\n" +
" \"name\": \"test3\",\n" +
" \"owner_id\": 37,\n" +
" \"owner_name\": \"test3\",\n" +
" \"project_id\": 37,\n" +
" \"update_time\": \"2021-04-06T01:48:12.097Z\"\n" +
"}]";
List<Map<String, Object>> list = JSON.parseObject(str, List.class);
Optional<Map<String, Object>> optional = list.stream().filter(map -> map.get("name").toString().equals("test3"))
.findFirst();
System.out.println(optional.get().get("project_id"));
}
}
public static void main(String[] args){
JSONArray jsonArray = JSON.parseArray("[{\n"
+ " \"creation_time\": \"2021-04-02T10:03:31.657Z\",\n"
+ " \"current_user_role_ids\": null,\n"
+ " \"cve_allowlist\": {\n"
+ " \"creation_time\": \"2021-04-02T10:03:31.660Z\",\n"
+ " \"id\": 34,\n"
+ " \"items\": [],\n"
+ " \"project_id\": 34,\n"
+ " \"update_time\": \"2021-04-02T10:03:31.660Z\"\n"
+ " },\n"
+ " \"metadata\": {\n"
+ " \"public\": \"false\"\n"
+ " },\n"
+ " \"name\": \"test1\",\n"
+ " \"owner_id\": 34,\n"
+ " \"owner_name\": \"test1\",\n"
+ " \"project_id\": 34,\n"
+ " \"repo_count\": 1,\n"
+ " \"update_time\": \"2021-04-02T10:03:31.657Z\"\n"
+ "}, {\n"
+ " \"creation_time\": \"2021-04-06T01:48:12.097Z\",\n"
+ " \"current_user_role_ids\": null,\n"
+ " \"cve_allowlist\": {\n"
+ " \"creation_time\": \"2021-04-06T01:48:12.098Z\",\n"
+ " \"id\": 37,\n"
+ " \"items\": [],\n"
+ " \"project_id\": 37,\n"
+ " \"update_time\": \"2021-04-06T01:48:12.098Z\"\n"
+ " },\n"
+ " \"metadata\": {\n"
+ " \"public\": \"false\"\n"
+ " },\n"
+ " \"name\": \"test3\",\n"
+ " \"owner_id\": 37,\n"
+ " \"owner_name\": \"test3\",\n"
+ " \"project_id\": 37,\n"
+ " \"update_time\": \"2021-04-06T01:48:12.097Z\"\n"
+ "}]");
for (int i = 0; i < jsonArray.size(); i++) {
// 用的是 fastJson
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
if ("test3".equals(name)) {
System.out.println(jsonObject.getString("project_id"));
}
}
}
JSONArray array = new JSONArray(json);
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
再循环下比对下就ok了
EventColAttr 是那个list集合里面的对象
你这个是数组呀,通过数组下标.属性就可以获取值了
你这个前端是用什么框架做的?
var arr = [{
"creation_time": "2021-04-02T10:03:31.657Z",
"current_user_role_ids": null,
"cve_allowlist": {
"creation_time": "2021-04-02T10:03:31.660Z",
"id": 34,
"items": [],
"project_id": 34,
"update_time": "2021-04-02T10:03:31.660Z"
},
"metadata": {
"public": "false"
},
"name": "test1",
"owner_id": 34,
"owner_name": "test1",
"project_id": 34,
"repo_count": 1,
"update_time": "2021-04-02T10:03:31.657Z"
}, {
"creation_time": "2021-04-06T01:48:12.097Z",
"current_user_role_ids": null,
"cve_allowlist": {
"creation_time": "2021-04-06T01:48:12.098Z",
"id": 37,
"items": [],
"project_id": 37,
"update_time": "2021-04-06T01:48:12.098Z"
},
"metadata": {
"public": "false"
},
"name": "test3",
"owner_id": 37,
"owner_name": "test3",
"project_id": 37,
"update_time": "2021-04-06T01:48:12.097Z"
}]
console.log(arr.filter(x=>x["name"]=="test3")[0].project_id)
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
亲 强烈安利JsonPath