Fórum

To extract data from jSON array

Kannan G krishnan, modificado 10 Anos atrás.

To extract data from jSON array

Regular Member Postagens: 121 Data de Entrada: 30/04/13 Postagens Recentes
Hi community,
I have a requirement in my project that i want to retrieve data from a JSON url like
localhost:8080/gwt-console-server/rs/process/instance/258/activeNodeInfo

I am trying to get the data from the jBPM workflow part. When i load this url i got the window with some display like,

[{"width":932,"height":541,"activeNode":{"name":"id\u003d13","x":662,"y":123,"width":118,"height":40}}]


I believe the values are there in the array. I want to store each of the values in different variables.

i used the code like,
try {
        	  List<string> result = new ArrayList<string>();
              String url = "http://localhost:8080/gwt-console-server/rs/process/instance/258/activeNodeInfo";
              String json = HttpUtil.decodePath(url);
              
                      JSONObject resultJsonObject = new JSONObject(json);
                      JSONArray jsonArray = resultJsonObject.getJSONArray("results");
                      System.out.println("jsonArray--&gt;"+jsonArray);
                      String name = null;
                      for (int index = 0, total = jsonArray.length(); index &lt; total; index++) {
                              final JSONObject jsonObject = jsonArray.getJSONObject(index);
                             
                              name = jsonObject.getString("name");
                              result.add(name);
                      }
                      System.out.println("result--&gt;"+result);
              } catch (JSONException e) {
                      throw new RuntimeException(e+"here");
              }</string></string>


But this is not working. How can i do this in liferay??

Regards
Kannan G.krishnan
thumbnail
meera prince, modificado 10 Anos atrás.

RE: To extract data from jSON array

Liferay Legend Postagens: 1111 Data de Entrada: 08/02/11 Postagens Recentes
Hi
You are getting response is JSON array so frst create JSON array object not JSON Object

Try following code
String json = HttpUtil.decodePath(url);
	JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
	for (int index = 0;index &lt; jsonArray.length(); index++) {
		JSONObject jsonObject = jsonArray.getJSONObject(index);
		System.out.println("width"+jsonObject.getString("width"));
		System.out.println("width"+jsonObject.getString("height"));
		JSONObject activeNodeJsonObject = jsonObject.getJSONObject("activeNode");
		System.out.println("Name"+activeNodeJsonObject.getString("name"));
		System.out.println("X"+activeNodeJsonObject.getString("x"));
		System.out.println("Y"+activeNodeJsonObject.getString("y"));
		
	}


Regards,
Meera Price