簡體   English   中英

如何從JSON響應中獲取所有屬性名稱(是否嵌套)

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

我有以下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"}}}

我無法遍歷每張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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM