简体   繁体   English

如何在JSON响应中的数组对象本身中的数组前面添加名称?

[英]How to add the name before the array in the array object itself in a JSON response?

I am consuming an external web service and receiving a JSON response. 我正在使用外部Web服务并收到JSON响应。 In this response, there is an object "entities" containing multiple arrays in it, with a name before each array. 在此响应中,有一个对象“实体”,其中包含多个数组,每个数组之前都有一个名称。

I want to add the name before the array in the array object itself. 我想在数组对象本身中的数组之前添加名称。

For example this is the original response: 例如,这是原始响应:

{

    "entities": {
        "entity": [
            {
                "confidence": 1,
                "value": "user",
                "type": "value"
            },
            {
                "confidence": 1,
                "value": "insurance form",
                "type": "value"
            }
        ],
        "ui_page_step": [
            {
                "confidence": 1,
                "value": "step 1",
                "type": "value"
            }
        ],
        "userrole_ano": [
            {
                "confidence": 0.96535832252792,
                "value": "anonymous user"
            }
        ]
    }
}

I need to convert it to: 我需要将其转换为:

{
  "entities": {
    "entity": [
      {
        "name": "entity",
        "confidence": 1,
        "value": "user",
        "type": "value"
      },
      {
        "name": "entity",
        "confidence": 1,
        "value": "insurance form",
        "type": "value"
      }
    ],
    "ui_page_step": [
      {
        "name": "ui_page_step",
        "confidence": 1,
        "value": "step 1",
        "type": "value"
      }
    ],
    "userrole_ano": [
      {
        "name": "userrole_ano",
        "confidence": 0.96535832252792,
        "value": "anonymous user"
      }
    ]
  }
}

How can I convert the original response to the desired one in Java? 如何在Java中将原始响应转换为所需的响应?

Here is a (one of several possible) solutions: 这是(几种可能的一种)解决方案:

  1. It uses Jackson library to parse the Json into a java Map that is (relatively) easier to navigate and modify than JSONObject. 它使用Jackson库将Json解析为一个Java Map ,相对于JSONObject,该Java Map (相对)更易于浏览和修改。

  2. the method putCollectionNamesInsideEntries() assumes one root "entities" entry that has several collections as values. 方法putCollectionNamesInsideEntries()假定一个根"entities"条目具有多个集合作为值。 it iterates over all of them, adding "name" entry with name of collection. 它遍历所有这些对象,并添加"name"条目和集合名称。

  3. the map is serialized back to Json (and sent to System.out ) 映射序列化回Json(并发送到System.out

     import java.io.*; import java.nio.file.*; import java.util.*; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTest { public static void main(String[] args) { try (InputStream is = Files.newInputStream(Paths.get("C:/temp/test.json"))) { ObjectMapper mapper = new ObjectMapper(); // deserialize json into map Map<String, Object> map = (Map<String, Object>)mapper.readValue(is, Map.class); putCollectionNamesInsideEntries(map); // serialize map into json mapper.writeValue(System.out, map); } catch (Exception e) { e.printStackTrace(); } } private static void putCollectionNamesInsideEntries(Map<String, Object> map) { // get root "entities" entry Map<String, Object> entitiesMap = (Map<String, Object>)map.get("entities"); for (Map.Entry<String, Object> entitiesEntry : entitiesMap.entrySet()) { // iterate over collection entries if (entitiesEntry.getValue() instanceof Collection) { Collection coll = (Collection)entitiesEntry.getValue(); // iterate over entries in collection for (Object collEntry : coll) { if (collEntry instanceof Map) { // add "name" with ame of collection (key entry under "entries") ((Map<String, Object>)collEntry).put("name", entitiesEntry.getKey()); } } } } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM