我正在调用一个返回json响应的方法。
呼叫:
String domainTermJson = getTermJson(domainTermId,client,gson);
Response:
{"assigned_assets":{"items":[{"_type":"database_column","_id":"b1c497ce.60641b50.42lla3a9f.di3kgbf.q6iaov.51q42k0equhf2ept2cgts","_url":"https://cs2cislp02.bns:9443/ibm/iis/igc-rest/v1/assets/b1c497ce.60641b50.42lla3a9f.di3kgbf.q6iaov.51q42k0equhf2ept2cgts","_name":"__AC_DEAACC"},{"_type":"database_column","_id":"b1c497ce.60641b50.42lla491l.o39k1ru.63p48q.57h74d2s5llcll4e429ji","_url":.
.
.
.etc...
我要做的是将所有_id值捕获到一个用逗号分隔的字符串中。示例如下:
String dc_id = "b1c497ce.60641b50.42lla3a9f.di3kgbf.q6iaov.51q42k0equhf2ept2cgts", "b1c497ce.60641b50.42lla491l.o39k1ru.63p48q.57h74d2s5llcll4e429ji"
如何使用json响应执行此操作?
答案 0 :(得分:0)
循环遍历JSON response like this并在每次迭代中将属性ID分配给字符串,如下所示:dc_id+=objectInArray.getString("_id");
答案 1 :(得分:0)
您可以使用JSONObject库来获取它。
JSONObject jsonObject = new JSONObject(getTermJson(domainTermId,client,gson));
JSONObject list = jsonObject.getJSONObject("assigned_assets").getJSONObject("items");
JSONArray arr = new JSONArray(list);
List<String> ids = new ArrayList<>();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
ids.add(obj.get("_id").toString());
}