0

Lately i have been trying to consume a Rest service which returns the below rough sample JSON. It has objects which contains Arrays and arrays contains Strings.

    {
   Main Object:{

      Object1:{
           }
      Object2:{
           }
      Object3:{
               Array1[String1,String2,String3]
               Array2[String1,String2,String3]
               Array3[String1,String2,String3]
               Array4[String1,String2,String3]
               }

    }}

My requirement is to get all the arrays and check which of the specific array contains specific/required String values and than get those strings to show in jsp.

I am using Spring MVC(Rest Template) but any java based solution would do the work.

P.S: I am New to WebServices.

6
  • 1
    Have a go yourself first and then post your code here so that we can try to help you fix it. Commented Dec 16, 2015 at 14:31
  • i have been trying Jackson, GSON, Java core and all tutorials i found on google but nothing worked for this situation. Commented Dec 16, 2015 at 14:36
  • I just need few lines of code by which i can just get the arrays separated, rest i know how to filter data once arrays are separated properly. Commented Dec 16, 2015 at 14:37
  • The number of objects (Object1, Object2) is fixed ? or it may change between calls ? Commented Dec 16, 2015 at 14:44
  • It may change, in fact there is lot of nesting. Commented Dec 16, 2015 at 15:53

1 Answer 1

1

use JSONArray to get it from below

JSONArray jsonArray = new JSONArray();

jsonArray = JSONObject.getJSONObject("Main Object").getJSONObject("Object3").getJSONArray("Array1");

//Iterate through the above array to get required String.

for the next array :

jsonArray = JSONObject.getJSONObject("Main Object").getJSONObject("Object3").getJSONArray("Array2");

//Iterate through the second array to get required String.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.