首页 > 解决方案 > 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();
  }  }

标签: java

解决方案


您必须访问路径Context[0].PersonNumber

这可以通过

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

推荐阅读