简体   繁体   中英

Get specific value from JSON array using java

I want to fetch only PersonNumber value from below JSON sample using java.

{"Context":[{"PersonNumber":"10","DMLOperation":"UPDATE","PeriodType":"E","PersonName":"Ponce","WorkerType":"EMP","PersonId":"1000","PrimaryPhoneNumber":"1","EffectiveStartDate":"2018-01-27","EffectiveDate":"2018-01-27"}],"Changed Attributes":[{"NamInformation1":{"new":"Ponce","old":"PONCE"}},{"FirstName":{"new":"Jorge","old":"JORGE"}},{"LastName":{"new":"Ponce","old":"PONCE"}}]}

Below is my code:

for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents()) {
              
 JSONObject jsonObj = null;
  try {
      jsonObj = new JSONObject(content.getValue().toString());
      System.out.println(jsonObj.get("Context"));
    
      } catch (JSONException e) {
      e.printStackTrace();
  }  }

You have to access to the path Context[0].PersonNumber .

This can be done with

String personNumber = jsonObj.getJSONArray("Context").getJSONObject(0).getString("PersonNumber");

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