gpt4 book ai didi

java - 从共享点解析json?

转载 作者:行者123 更新时间:2023-11-30 05:48:23 25 4
gpt4 key购买 nike

我收到了来自 sharepoint 的回复。

{
"d": {
"query": {
"SecondaryQueryResults": {
"__metadata": {
"type": "Collection(Microsoft.Office.Server.Search.REST.QueryResult)"
},
"results": []
},
"SpellingSuggestion": "",
"TriggeredRules": {
"__metadata": {
"type": "Collection(Edm.Guid)"
},
"results": ["e0205660-4971-4574-aa40-af6b4383cadd"]
},
"ElapsedTime": 224,
"__metadata": {
"type": "Microsoft.Office.Server.Search.REST.SearchResult"
},
"Properties": {
"__metadata": {
"type": "Collection(SP.KeyValue)"
},
"results": [{
"ValueType": "Edm.Int32",
"Value": "10",
"Key": "RowLimit"
}, {
"ValueType": "Edm.Guid",
"Value": "8413cd39-2156-4e00-b54d-11efd9abdb49",
"Key": "SourceId"
}, {
"ValueType": "Edm.Guid",
"Value": "7bc4ba9e-80ff-7000-58cf-f7ac556d1e34",
"Key": "CorrelationId"
}, {
"ValueType": "Edm.Boolean",
"Value": "false",
"Key": "WasGroupRestricted"
}, {
"ValueType": "Edm.Boolean",
"Value": "false",
"Key": "IsPartial"
}, {
"ValueType": "Edm.Boolean",
"Value": "false",
"Key": "HasParseException"
}, {
"ValueType": "Edm.String",
"Value": "en",
"Key": "WordBreakerLanguage"
}, {
"ValueType": "Edm.Boolean",
"Value": "false",
"Key": "IsPartialUpnDocIdMapping"
}, {
"ValueType": "Edm.Boolean",
"Value": "true",
"Key": "EnableInterleaving"
}, {
"ValueType": "Edm.Boolean",
"Value": "false",
"Key": "IsMissingUnifiedGroups"
}, {
"ValueType": "Edm.String",
"Value": "i62456",
"Key": "Constellation"
}, {
"ValueType": "Edm.String",
"Value": "<Query Culture=\"en-US\" EnableStemming=\"True\" EnablePhonetic=\"False\" EnableNicknames=\"False\" IgnoreAllNoiseQuery=\"True\" SummaryLength=\"180\" MaxSnippetLength=\"180\" DesiredSnippetLength=\"90\" KeywordInclusion=\"0\" QueryText=\"59055305\" QueryTemplate=\"\" TrimDuplicates=\"True\" Site=\"e297bd2b-597a-4f54-8509-e2febb91b869\" Web=\"d42ff0d1-883b-4545-ab6a-97b0401025d4\" KeywordType=\"True\" HiddenConstraints=\"\" \/>",
"Key": "SerializedQuery"
}]
},
"PrimaryQueryResult": {
"RefinementResults": null,
"SpecialTermResults": null,
"QueryId": "0585a5f1-89bc-43c1-b736-e163b4d7c1dd",
"QueryRuleId": "00000000-0000-0000-0000-000000000000",
"CustomResults": {
"__metadata": {
"type": "Collection(Microsoft.Office.Server.Search.REST.CustomResult)"
},
"results": []
},
"__metadata": {
"type": "Microsoft.Office.Server.Search.REST.QueryResult"
},
"RelevantResults": {
"Table": {
"__metadata": {
"type": "SP.SimpleDataTable"
},
"Rows": {
"results": [{
"__metadata": {
"type": "SP.SimpleDataRow"
},
"Cells": {
"results": [{
"ValueType": "Edm.Double",
"Value": "26.8860855102549",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Rank"
}, {
"ValueType": "Edm.Int64",
"Value": "17594532057853",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "DocId"
}, {
"ValueType": "Edm.Int64",
"Value": "17594531057253",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "WorkId"
}, {
"ValueType": "Edm.String",
"Value": "Customer Request Filling",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Title"
}, {
"ValueType": "Edm.String",
"Value": "Technology Services;svc ECMWise",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Author"
}, {
"ValueType": "Edm.Int64",
"Value": "97182",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Size"
}, {
"ValueType": "Edm.String",
"Value": "https:\/\/xxxxxx.sharepoint.com\/sites\/news\/CUST\/Forms\/Appeals\/Customer Reader.pdf",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Path"
}, {
"ValueType": "Null",
"Value": null,
"__metadata": {
"type": "SP.KeyValue"
},
....

我想要获取的是该 pdf 的“值”字符串的“路径”作为下面的快照(上面部分的一部分)

{
"ValueType": "Edm.String",
"Value": "https:\/\/xxxxxx.sharepoint.com\/sites\/news\/CUST\/Forms\/Appeals\/Customer Reader.pdf",
"__metadata": {
"type": "SP.KeyValue"
},
"Key": "Path"
}

我尝试过使用 JSONObject 像这样解析它。

            JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader((httpConn.getInputStream())));      
JSONObject folder = (JSONObject)jsonObject.get("d");
JSONObject query = (JSONObject) folder.get("query");
JSONObject properties = (JSONObject) query.get("PrimaryQueryResult");
JSONObject result = (JSONObject) properties.get("RelevantResults");
JSONObject table = (JSONObject) result.get("Table");
JSONObject rows = (JSONObject) table.get("Rows");
....

我想知道是否有更简单的方法来执行此操作,或者如果其中一个键未填充怎么办。它会抛出空指针异常,所以有没有办法直接在这个长嵌套的 json 中找到一个键,以及如何从“Cells”的 json 数组中获取字段“Key”

最佳答案

您应该考虑使用 JSON 序列化程序,例如 Jackson 或 GS​​ON。

如果每次调用的底层 JSON 响应结构都相同,即使它缺少键,您也可以使用类似 JSONSchema2POJO 的内容创建自己的 POJO 。这将使用您提供的 JSON 字符串生成带有 Jackson 或 GS​​ON 注释的 POJO。以下是使用 JSON 字符串片段的示例:

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("ValueType")
@Expose
private String valueType;
@SerializedName("Value")
@Expose
private String value;
@SerializedName("__metadata")
@Expose
private Metadata metadata;
@SerializedName("Key")
@Expose
private String key;

public String getValueType() {
return valueType;
}

public void setValueType(String valueType) {
this.valueType = valueType;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Metadata getMetadata() {
return metadata;
}

public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

}
-----------------------------------com.example.Metadata.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Metadata {

@SerializedName("type")
@Expose
private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}

这是在 Java 中使用 JSON 时的标准做法,并且在序列化对象时允许键/值为 null,而不会破坏代码。

如果 JSON 结构完全不可预测,GSON 允许您创建 Generic types或者你可以更进一步 write custom serializers 。 Jackson 可能有类似的功能,但我只精通 GSON。

关于java - 从共享点解析json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54424034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com