如何在java中使用jersy读取json数组中的json对象?

时间:2017-02-02 04:50:15

标签: java rest jersey

Here is my JSON array.
{ "pidValues":
[{"PID":"12344","associatedSubFeatureID":"1","dashboardName":"CWC"},
{"PID":"12345","associatedSubFeatureID":"1","dashboardName":"WSM"}] 
}

我需要在String数组中检索PID的值。 我正在使用泽西框架。

2 个答案:

答案 0 :(得分:0)

JSONObject data = new JSONObject(your_JSON_Repsonse);
JSONArray data_pidValues=data.getJSONArray(pidValues);
for(int i=0;i<=data_pidValues.length();i++)
{
 PID=data_pidValues.getString("PID");
associatedSubFeatureID=data_pidValues.getString("associatedSubFeatureID");
dashboardName=data_pidValues.getString("dashboardName");
}

答案 1 :(得分:0)

    As I am using jersey the method you suggested above doesn't work. I used below code to parse.
@Path("/any")
@Consumes("application/json")    
public String getAllData(JSONArray pidValues)
    {
    String [] accessiblepid = new String[pidValues.size()];
    LinkedHashMap<String, String> mapList = new LinkedHashMap<String, String>();

            String subProgId="";
            JSONArray jsonArray = new JSONArray();
            JSONArray jsonArrayPid = new JSONArray();

            for (int i=0; i<pidValues.size();i++)
                {

                System.out.println(pidValues.get(i));
                mapList =(LinkedHashMap<String, String>) pidValues.get(i);
                System.out.println(mapList.get("PID"));
                accessiblepid[i] = mapList.get("PID").toString();
                jsonArray.add(mapList.get("dashboardName").toString());
                jsonArrayPid.add(mapList.get("PID").toString());
                subProgId=mapList.get("associatedSubFeatureID");
                }
        return "";
    }