簡體   English   中英

如何在JSON響應中的數組對象本身中的數組前面添加名稱?

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

我正在使用外部Web服務並收到JSON響應。 在此響應中,有一個對象“實體”,其中包含多個數組,每個數組之前都有一個名稱。

我想在數組對象本身中的數組之前添加名稱。

例如,這是原始響應:

{

    "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"
            }
        ]
    }
}

我需要將其轉換為:

{
  "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"
      }
    ]
  }
}

如何在Java中將原始響應轉換為所需的響應?

這是(幾種可能的一種)解決方案:

  1. 它使用Jackson庫將Json解析為一個Java Map ,相對於JSONObject,該Java Map (相對)更易於瀏覽和修改。

  2. 方法putCollectionNamesInsideEntries()假定一個根"entities"條目具有多個集合作為值。 它遍歷所有這些對象,並添加"name"條目和集合名稱。

  3. 映射序列化回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