如何读取类似结构的数组并按规定制作键值对? P.S 请检查正文以便更好地解释查询

How to read a array like structure and make a key-value pair as specified? P.S Please check body for better explaination of query

[
target {
  column: "marks"
}
parents {
  table {
    kind: BIGQUERY_TABLE
    sqlResource: "bigquery.table.location"
  }
}

, 

target {
  column: "name"
}
parents {
  table {
    kind: BIGQUERY_TABLE
    sqlResource: "bigquery.table.location"
    
  }
}
]

它是一个类似输入的数组(逗号分隔),我想从 bigquery table(bqtable 的位置 table 中读取列(如 'target:column' 下指定=15=] 并存储在一个键值对中,应该怎么做呢? 我尝试使用 if-else 条件,我觉得应该有一种有效的方法来做到这一点? 请让我知道更好的解决方案。

你的问题分为两部分:

  1. 如何解析您提供的字符串
  2. 如何使用解析后的信息

我会回答第一部分。您提供的字符串看起来像 JSON 字符串,但它是无效的 JSON。在键“target”、“parents”和“table”之后必须有符号“:”。这将使它成为一个有效的 JSON。假设你有这个。您可以使用任何可用的 JSON 库(特别是 Json-Jackson, (Maven artifacts here) or Gson)解析您的 JSON。我编写了自己的名为 MgntUtils 的开源库,它具有基于 Json-Jackson 的 JSON 解析器。使用此库,您可以轻松地将 JSON 字符串解析为地图列表

List<Map<String, Object>> list = null;
try {
      list = JsonUtils.readObjectFromJsonString(jsonString, List.class);
    }
} catch (IOException e) {
   ...
}

您的列表将包含两个 Map 元素,在“target”键下您将有一个带有键“column”及其值等的映射。这是相关的 Javadoc. The MgntUtils Maven artifacts could be found here and Github project with source code and javadoc is here