java - 如何从 JSON 响应中获取所有属性名称(嵌套或非嵌套)

标签 java json dictionary

我有以下 json 响应。我无法迭代每个Map。请帮助我

{"status":"OK","result":{"1":{"Id":"3","Conferencce":"test3","Description":"test3","Admin":"919818559890","Moderator":null,"Keywords":"test3","StartDate":"2011-11-19 12:22:33","EndDate":"2011-11-19 14:22:33","Type":"both","MaxAtendee":"0","MinAtendee":"0","RegAtendee":"0","DescVoiceVideo":null,"Rating":null,"Status":"active","ApproveBy":null,"ApprovedOn":"2011-11-15 14:22:33","ApprovedReason":null,"AdminPin":null,"UserPin":null,"PricePerMin":null,"PricePerConf":null,"ReminderStart":null,"AdminJoin":null,"CreatedOn":"2011-11-17 13:31:27","CreatedBy":"1"},"2":{"Id":"2","Conferencce":"test2","Description":"test","Admin":"919818559899","Moderator":null,"Keywords":"test2","StartDate":"2011-11-18 12:22:33","EndDate":"2011-11-18 14:22:33","Type":"both","MaxAtendee":"0","MinAtendee":"0","RegAtendee":"0","DescVoiceVideo":null,"Rating":null,"Status":"active","ApproveBy":null,"ApprovedOn":"2011-11-15 12:22:33","ApprovedReason":null,"AdminPin":null,"UserPin":null,"PricePerMin":null,"PricePerConf":null,"ReminderStart":null,"AdminJoin":null,"CreatedOn":"2011-11-17 13:31:20","CreatedBy":"1"},"3":{"Id":"1","Conferencce":"test","Description":"tes","Admin":"919818559898","Moderator":null,"Keywords":"test","StartDate":"2011-11-17 12:22:33","EndDate":"2011-11-17 14:22:33","Type":"both","MaxAtendee":"0","MinAtendee":"0","RegAtendee":"0","DescVoiceVideo":null,"Rating":null,"Status":"active","ApproveBy":"1","ApprovedOn":"2011-11-15 12:22:33","ApprovedReason":null,"AdminPin":null,"UserPin":null,"PricePerMin":null,"PricePerConf":null,"ReminderStart":null,"AdminJoin":null,"CreatedOn":"2011-11-17 13:31:15","CreatedBy":"1"}}}

最佳答案

I am not able to iterate through each Map

如果键的名称一致且不变,我不会将 result 响应的三个组成部分视为映射,而是定义一个 Java 对象来匹配整体数据结构,如下所示线。请注意,此示例使用 Jackson处理 JSON 到 Java 的转换。

import java.io.File;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(JsonMethod.ALL, Visibility.ANY);
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    Response response = mapper.readValue(new File("input.json"), Response.class);

    for (Map.Entry<Integer, Result> entry : response.result.entrySet())
    {
      System.out.printf("Entry %1$d: %2$s\n", entry.getKey(), entry.getValue());
    }
  }
}

class Response
{
  ResponseStatus status;
  Map<Integer, Result> result;
}

enum ResponseStatus
{
  OK, NOT_OK
}

class Result
{
  int Id;
  String Conferencce;
  String Description;
  BigInteger Admin;
  String Moderator;
  String Keywords;
  Date StartDate;
  Date EndDate;
  String Type;
  int MaxAtendee;
  int MinAtendee;
  int RegAtendee;
  String DescVoiceVideo;
  String Rating;
  Status Status;
  String ApproveBy;
  Date ApprovedOn;
  String ApprovedReason;
  String AdminPin;
  String UserPin;
  String PricePerMin;
  String PricePerConf;
  String ReminderStart;
  String AdminJoin;
  Date CreatedOn;
  int CreatedBy;

  @Override
  public String toString()
  {
    return String.format("Id: %1$d, Conferencce: %2$s, Description: %3$s, Admin: %4$d, StartDate: %5$tY-%5$tm-%5$td %5$tH:%5$tM:%5$tS", Id, Conferencce, Description, Admin, StartDate);
  }
}

enum Status
{
  active, inactive
}

关于java - 如何从 JSON 响应中获取所有属性名称(嵌套或非嵌套),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182814/

相关文章:

java - 如何使用 protobuf-java-format 保留未知字段

php - 如何允许其他站点从您的站点检索数据?

python - 使用python访问函数内部的字典

java - Android 中的下一个/后退按钮可检索下一个/上一个记录

java - 我如何报告测试结果并使用 maven/junit/spock 按功能编号对它们进行分组?

java - Steam 社区好友列表并向他们发送消息

json - 如何在Elasticsearch中进行部分匹配?

java - 通过 revalidate() 和 repaint() 更新 Swing 中的 JMenu 不起作用

arrays - 将字典值附加到 Swift 中的数组

python - 查看 python 字典的字典中是否存在键列表