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

huangapple go评论62阅读模式
英文:

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();
    }
}

(Note: The code provided here is the same as the original code you posted, but with the HTML entities replaced with their respective characters for better readability.)

英文:

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

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

Below is my code:

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

答案1

得分: 1

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

可以通过以下方式实现:

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

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

This can be done with

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

huangapple
  • 本文由 发表于 2020年9月23日 23:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定