简体   繁体   English

使用java从JSON数组中获取特定值

[英]Get specific value from JSON array using java

I want to fetch only PersonNumber value from below JSON sample using java.我想使用 java 从下面的 JSON 示例中仅获取 PersonNumber 值。

{"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 .您必须访问路径Context[0].PersonNumber

This can be done with这可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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