我有以下 JSON 字符串,我只需要提取 id 字段和名称字段的列表, ID的预期输出为:
{15124,10287,13766,13568...etc}
名称的预期输出为:
{" yoav (yoavshaki@yahoo.com) - 301519506662355","Inactive (ilan.dayan)","Inactive (joelpeleh@yahoo.com)","Inactive (jonatan.silvester)"..etc}
我尝试了这条路径:$.response.data.*.id
但是我得到了"时区"键内的ID列表。我不需要。
这是JSON字符串:
{ "response": {
"code": 200,
"msg": "Success",
"data": [
{
"id": 15124,
"name": " yoav (yoavshaki@yahoo.com) - 301519506662355",
"network_id": 301519506662355,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 2,
"facebook_code": 70
},
"owner": {
"name": "Ido Levi",
"email": "yoavshaki@yahoo.com",
"network_id": 100004129233318,
"network_type": "Facebook"
}
},
{
"id": 10287,
"name": "Inactive (ilan.dayan)",
"network_id": 111979298917521,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 2,
"facebook_code": 70
},
"owner": {
"name": "Ilan Sloto",
"email": "ilan.dayan@walla.com",
"network_id": 100003163553895,
"network_type": "Facebook"
}
},
{
"id": 13766,
"name": "Inactive (joelpeleh@yahoo.com)",
"network_id": 1405567046335275,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 2,
"facebook_code": 70
},
"owner": {
"name": "Joel Peleh",
"email": "joelpeleh@yahoo.com",
"network_id": 100006459791435,
"network_type": "Facebook"
}
},
{
"id": 13568,
"name": "Inactive (jonatan.silvester)",
"network_id": 125737960970262,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
},
"owner": {
"name": "Jonatan Silvester",
"email": "jonatan.silvester@gmail.com",
"network_id": 100006021764102,
"network_type": "Facebook"
}
},
{
"id": 13472,
"name": "Inactive (markus.sogova)",
"network_id": 127771634098217,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
},
"owner": {
"name": "Marcus Sogova",
"email": "markus.sogova@gmail.com",
"network_id": 100005961429701,
"network_type": "Facebook"
}
},
{
"id": 13052,
"name": "Inactive (matan.hamesh)",
"network_id": 110032855847424,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 2,
"facebook_code": 70
},
"owner": {
"name": "",
"email": "matan.hamesh@walla.com",
"network_id": 100005221303485,
"network_type": "Facebook"
}
},
{
"id": 13010,
"name": "Inactive (matan.shmone)",
"network_id": 110145245840590,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 2,
"facebook_code": 70
},
"owner": {
"name": "",
"email": "matan.shmone@walla.com",
"network_id": 100005353451967,
"network_type": "Facebook"
}
},
{
"id": 13726,
"name": "Inactive (matinsonk@rediffmail.com)",
"network_id": 1406755166207011,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
},
"owner": {
"name": "Venita Vagt",
"email": "matinsonk@rediffmail.com",
"network_id": 100006176232384,
"network_type": "Facebook"
}
},
{
"id": 15134,
"name": "yoav (zoltantimov@gmx.com) - 1388742651379471",
"network_id": 1388742651379471,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 10,
"code": "CET",
"region": "Europe",
"locality": "Sofia",
"offset": 3,
"facebook_code": 19
},
"owner": {
"name": "Zoltan Timov",
"email": "zoltantimov@gmx.com",
"network_id": 100007312476197,
"network_type": "Facebook"
}
}
]
} }
我应该发送什么 jsonpath 模式?
答案 0 :(得分:0)
使用$.response.data.*.id
模式,您要求 data 节点下的所有* id * s,其中包含 timezone id。
要使用的正确模式是$.response.data.[*].id
和$.response.data.[*].name
,即选择所有 id ( name )值,它们是...的直接兄弟数据节点。
以下代码段将说明此案例:
public class ResponseService
{
public static void main(String[] args)
{
final String OPENING_CURLY = "{";
final String CLOSING_CURLY = "}";
final String COLON = ",";
final String QUOTE = "\"";
String jsonData; // Load you response json feed.
List<Object> ids = JsonPath.read(jsonData, "$.response.data[*].id");
StringBuilder formattedIds = new StringBuilder(OPENING_CURLY);
for (Iterator it = ids.iterator(); it.hasNext();)
{
formattedIds.append(it.next());
if (it.hasNext())
{
formattedIds.append(COLON);
}
}
formattedIds.append(CLOSING_CURLY);
System.out.println(formattedIds);
List<Object> names = JsonPath.read(jsonData, "$.response.data[*].name");
StringBuilder formattedNames = new StringBuilder(OPENING_CURLY);
for (Iterator it = names.iterator(); it.hasNext();)
{
formattedNames.append(QUOTE)
.append(it.next())
.append(QUOTE);
if (it.hasNext())
{
formattedNames.append(COLON);
}
}
formattedNames.append(CLOSING_CURLY);
System.out.println(formattedNames);
}
}
以上代码将导致以下输出:
{15124,10287,13766,13568,13472,13052,13010,13726,15134}
{" yoav (yoavshaki@yahoo.com) - 301519506662355","Inactive (ilan.dayan)","Inactive (joelpeleh@yahoo.com)","Inactive (jonatan.silvester)","Inactive (markus.sogova)","Inactive (matan.hamesh)","Inactive (matan.shmone)","Inactive (matinsonk@rediffmail.com)","yoav (zoltantimov@gmx.com) - 1388742651379471"}
答案 1 :(得分:0)
使用json.org的简单方法
String str ="yourJsonString";
List<String> listOfIds = new ArrayList<String>();
JSONObject jsonObject =new JSONObject(str);
String jsonStr = jsonObject.get("response").toString();
JSONObject jsonObj =new JSONObject(jsonStr);
String jsonS = jsonObj.get("data").toString();
JSONArray jsonArray = new JSONArray(jsonS);
for(int i =0;i<jsonArray.length();i++){
JSONObject object = new JSONObject(jsonArray.get(i).toString());
listOfIds.add(object.get("id").toString());
}
System.out.println(listOfIds);