使用java迭代JSON响应中的最后一个值

时间:2017-01-30 07:52:53

标签: java json gson

我需要迭代并从JSON响应中获取最后一个值,如name,url和color。使用java / gson api。请帮帮我。

    {
  "Title": {
    "desc": [
      {
        "name": "PRE_DB",
        "url": "http://jenkins.example.com/job/my_first_job/",
        "color": "blue_anime"
      },
      {
        "name": "SDD_Seller_Dashboard",
        "url": "http://jenkins.example.com/job/my_second_job/",
        "color": "blue_anime"
      }
    ]
  }
}

示例输出: 名称:SDD_Seller_Dashboard 颜色:blue_anime

1 个答案:

答案 0 :(得分:1)

JSONObject data = new JSONObject(your_JSON_Repsonse);
JSONArray data_desc=data.getJSONArray(desc);
for(int i=0;i<=data_desc.length();i++)
{
 name=data_desc.getString("name");
url=data_desc.getString("url");
color=data_desc.getString("color");
}