简体   繁体   English

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

[英]How to get all attributes names(nested or not) in from JSON response

I have following json response. 我有以下json响应。 I am not able to iterate through each Map . 我无法遍历每个Map Please help me 请帮我

{"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 我无法遍历每张Map

Instead of treating the three components of the result response as maps, if the names of the keys are consistent and unchanging, I'd define a Java object to match the overall data structure along the following lines. 如果键的名称是一致且不变的,而不是将result响应的三个组成部分视为映射,则我将定义一个Java对象以按照以下几行来匹配总体数据结构。 Note this example uses Jackson to handle the JSON-to-Java conversion. 请注意,此示例使用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
}

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

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