如何在json数组中获取内部数组的键的值

时间:2019-05-12 17:16:34

标签: java json gson

这是我要解析为json并获取“ s”,“ o”,“ c”和“ p”值的字符串。

{
    "head": {
        "vars": [
            "s",
            "c",
            "o",
            "p"
        ]
    },
    "results": {
        "bindings": [
            {
                "s": {
                    "type": "uri",
                    "value": "http://example.org/data/window"
                },
                "c": {
                    "type": "uri",
                    "value": "http://www.w3.org/ns/sosa/FeatureOfInterest"
                },
                "o": {
                    "type": "uri",
                    "value": "http://example.org/data/window104state"
                },
                "p": {
                    "type": "uri",
                    "value": "http://www.w3.org/ns/ssn/hasProperty"
                }
            },
                        {
                "s": {
                    "type": "uri",
                    "value": "http://example.org/data/earth"
                },
                "c": {
                    "type": "uri",
                    "value": "http://www.w3.org/ns/sosa/FeatureOfInterest"
                },
                "o": {
                    "type": "uri",
                    "value": "http://example.org/data/VCAB-DP1-BP-40location"
                },
                "p": {
                    "type": "uri",
                    "value": "http://www.w3.org/ns/sosa/hasSample"
                }
            }
        ]
    }
}

这是我到目前为止尝试过的代码:

JsonParser jsonParser =  new JsonParser();
JsonElement element = jsonParser.parse(str);
JsonObject obj = element.getAsJsonObject();
JsonObject results = obj.get("results").getAsJsonObject();
for(Map.Entry<String, JsonElement> entry : results.entrySet()) {
            JsonArray array = entry.getValue().getAsJsonObject().getAsJsonArray("bindings");
            for (JsonElement jsonElement : array) {
                 for (Map.Entry<String, JsonElement> entry1 : jsonElement.getAsJsonObject().entrySet()) {
                     System.out.println("Key = " + entry1.getKey() + " Value = " + entry1.getValue() );
                }
            }

我想要得到的是内部数组的值,例如: “ s”:“ http://example.org/data/earth” “ c”:“ http://www.w3.org/ns/sosa/FeatureOfInterest” 等等 相反,我得到一个错误:

Exception in thread "main" java.lang.IllegalStateException: Not a JSON Object: [{"s":{"type":"uri","value":"http://example.org/data/window"},"c":{"type":"uri","value":"http://www.w3.org/ns/sosa/FeatureOfInterest"},"o":{"type":"uri","value":"http://example.org/data/window104state"},

(整个字符串)。

更新 感谢@Deadpool,我设法获取了值,但是现在我需要获取绑定的“内部”值,这意味着每个绑定(s,c,p和o)的“值”部分。我只需要这一部分,而无需“类型”部分。 这是@Deadpool的结果:

Key = s Value = {"type":"uri","value":"http://example.org/data/window"}
Key = c Value = {"type":"uri","value":"http://www.w3.org/ns/sosa/FeatureOfInterest"}
Key = p Value = {"type":"uri","value":"http://www.w3.org/ns/ssn/hasProperty"}
Key = o Value = {"type":"uri","value":"http://example.org/data/window104state"}

解决方案 好吧,对于那些感兴趣的人,我设法获得了所需的陈述:

System.out.println("Key = " + entry1.getKey() + " Value = " + entry1.getValue().getAsJsonObject().get("value"));

这是预期的结果:

Key = s Value = "http://example.org/data/earth"
Key = c Value = "http://www.w3.org/ns/sosa/FeatureOfInterest"
Key = o Value = "http://example.org/data/VCAB-DP1-BP-40location"
Key = p Value = "http://www.w3.org/ns/sosa/hasSample"

2 个答案:

答案 0 :(得分:4)

问题在于此语句bindingsJsonArray,直接以JsonArray的方式获得

JsonArray array = entry.getValue().getAsJsonObject().getAsJsonArray("bindings");

解决方案

for(Map.Entry<String, JsonElement> entry : results.entrySet()) {
        JsonArray array = entry.getValue().getAsJsonArray();
        for (JsonElement jsonElement : array) {
             for (Map.Entry<String, JsonElement> entry1 : jsonElement.getAsJsonObject().entrySet()) {
                 System.out.println("Key = " + entry1.getKey() + " Value = " + entry1.getValue() );
            }
        }

答案 1 :(得分:0)

您可以使用Declarative Stream Mapping (DSM)流解析库轻松地从XML或JSON捕获数据

首先,您必须以yaml或JSON格式定义JSON数据和字段之间的映射。

以下是映射定义:

result:     
   type: array
   path: /results/bindings/(s|c|o|p)   // regex
   fields:
     key:
       path: type
     value:  

您要反序列化的Java类:

public class KeyValue{
    public String key;
    public String value
}   

用于解析JSON的Java代码:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).create(KeyValue.class);
List<KeyValue> list=  (List<KeyValue>)dsm.toObject(jsonData);
// write root object as json
dsm.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(System.out, list);

此处输出:

    [ {
  "key" : "uri",
  "value" : "http://example.org/data/window"
}, {
  "key" : "uri",
  "value" : "http://www.w3.org/ns/sosa/FeatureOfInterest"
}, {
  "key" : "uri",
  "value" : "http://example.org/data/window104state"
}, {
  "key" : "uri",
  "value" : "http://www.w3.org/ns/ssn/hasProperty"
}, {
  "key" : "uri",
  "value" : "http://example.org/data/earth"
}, {
  "key" : "uri",
  "value" : "http://www.w3.org/ns/sosa/FeatureOfInterest"
}, {
  "key" : "uri",
  "value" : "http://example.org/data/VCAB-DP1-BP-40location"
}, {
  "key" : "uri",
  "value" : "http://www.w3.org/ns/sosa/hasSample"
} ]
相关问题