Hey amarnath1234,
If you are really good in handling JSON using Gson , jettison and json-simple, then it's quite easier for you to do it. You can use simple java code to handle this.
- Get the Json
- Convert to JSONArray of "personalizedOffers" and iterate using for loop till the size() of array.
- Convert the Array Data to JSONObject of "promotion" and again iterate using for loop till the "promotion" object size.
- Fetch all the String values you require, which exists inside promotion object.
This is the way, you can handle this dynamically and you don't have to worry about the no. of "promotion" object existing.
Here is the working code:-
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Desktop\\json.txt"));
String json = "";
try {
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = reader.readLine();
}
json = sb.toString();
} finally {
reader.close();
}
JSONObject object = new JSONObject(json);
JSONArray personalizedOffers = new JSONArray(object.getString("personalizedOffers"));
for(int i=0; i<personalizedOffers.length(); i++) {
JSONObject validCoupon = (JSONObject) personalizedOffers.getJSONObject(i).get("validCoupon");
JSONObject promotion = validCoupon.getJSONObject("promotion");
System.out.println(promotion.getString("id"));
System.out.println(promotion.getString("createdTime"));
System.out.println(promotion.getString("lastUpdatedTime"));
System.out.println(promotion.getString("shortDescription"));
System.out.println(promotion.getString("longDescription"));
}
If you are using ReadyAPI, please download - jettison.jar, put inside soapui/home/ext and then run this code.
Please refer the link I have mentioned above. First take example of easy JSON and then try the JSON you have mentioned here.
If the code works, please don't forget to Accept as solution.
Thanks,
avidCoder