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

时间:2011-11-18 12:59:49

标签: java json map

我有跟随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"}}}

1 个答案:

答案 0 :(得分:1)

  

我无法遍历每个Map

不是将result响应的三个组件视为映射,如果键的名称一致且不变,我将定义一个Java对象以匹配沿着以下行的整体数据结构。请注意,此示例使用Jackson来处理JSON-to-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
}