简体   繁体   中英

Having trouble accessing “endpoints” from below Json

I want all the URL present under the " endpoints" json.

I tried with access it through converting the String to JSON and after that using JSON parsar. But couldn't successed.


 {  
 "process":{  
  "id":"epsswitch/process/create-switch-bdl",
     "views":{  
        "selection":{  
          "uri":"//scp/sdf/sdf/s/df",
          "controller":"scp/switching/createswitchingbdl/controllers/selection"
        },
     "customers":{  
        "uri":"//scp/vv/vv/views/Customers",
        "controller":"scp/x/vc/controllers/customers"
     },
     "instructions":{  
        "uri":"//scp/df/fd/views/Information",
        "controller":"scp/switching/fd/controllers/instructions"
     },
     "confirm":{  
        "uri":"//scp/switching/createswitchingbdl/views/Confirmation",
        "controller":"scp/switching/createswitchingbdl/controllers/confirm"
     }
  },
  "endpoints":{  
     "data":{  
        "uri":"/epsswitch/create/data?cc=true&al=true&ac=true"
     },
     "bank":{  
        "uri":"/fdd/df/df/v1/bank/df/",
        "method":"GET"
     }
  }   
}

There are multiple ways you can parse an JSON string. I use json simple (org.json.simple), java json are some of them. The following code uses the latter. It takes all the keys from the json string and retrieves all the values inside each key.

String sJSONString = "<your json object here>";
JSONObject jsObjects   = new JSONObject(sJSONString);

//This is where you get your keys
Iterator<String> keys = jsObjects.keys();

//Use while if you need to poll for each key in JSON string
while(keys.hasNext())
{
    String keyN = keys.next();

    JSONObject jsoObjN = new JSONObject();

    jsoObjN = (JSONObject)jsObjects.get(keyN);

    //<Your logic here>
}

Following code help me to achive this.

final JsonNode studentNode = mapper.readTree(sPhone.getProcessJson());
JsonNode process = studentNode.get("process");
JsonNode endpoints = process.get("endpoints");
  for (JsonNode jsonNode : endpoints) {
        JsonNode uri = jsonNode.get("uri");
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM