2

I am new To JSon and i want to search the following json string and get the required output. String:

{"status":"Success","code":"200","message":"Retrieved Successfully","reason":null,"
 "projects":
   [
       {
           "projectName": "example",
           "users":
           [
               {
                   "userName": "xyz",
                   "executions":
                   [
                       {
                           "status": "check",
                           "runs":
                           [
                               {
                                   "Id": "------",
                                   "Key": "---"
                               }
                           ],
                           "RCount": 1
                       }
                   ],
                   "RCount": 1
               }
           ],
           "RCount": 1
       },

Like that i have many projects and now , if i give projectname and username as input i wantt to get its status as output. Is it possible?If yes how?

user5413491
  • 43
  • 1
  • 7
  • 1
    You have to use a Json Parser. You can find more details here: http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Hedi Ayed Nov 26 '15 at 07:01

3 Answers3

2

You may use JSONObject for this.

JSONObject json = new JSONObject(string);

JSONArray[] projectsArray = json.getJSONArray("projects");

for(int i = 0; i < projectsArray.length; ++i)
{
  String projectName = projectsArray[i].getString("projectName");
  ...
}

Use the same method to get the users.

Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

You can use gson library. Using gson convert your json string to Map and then you can iterate through map to get required item

Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(jsonString, type);
Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27
0

You can use the Google gson to map your json data structure to a Java POJOs. Example : You can have Projects class containing list/array of Users. Users class containing list/array of Executions and so on.

Gson library can easily map the json to these classes as objects and you can access your data in a more elegant manner.

Here are a few references :

arnabkaycee
  • 1,634
  • 13
  • 26