2016-10-01 15 views
-3

に私は次のjsonStringを持って、私は、Javaオブジェクトに変換する必要があり、私はそれを変換するために、次のコードを使用しているが、私は変換のJSON文字列Javaオブジェクト

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.innvo.web.rest.dto.Responsedetail$Questiongroup]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) 

JSON文字列

この例外を取得
String jsonStrings ="{"questiongroups":[{"questiongroup":"1000","questions":[{"question":1001,"response":4},{"question":1002,"subquestion":1001,"response":"A2"}]}]}"; 

ResponseDetails.java

public class Responsedetail { 

@JsonProperty("questiongroups") 
public List<Questiongroup> questiongroups; 

public List<Questiongroup> getQuestiongroups() { 
    return questiongroups; 
} 

public void setQuestiongroups(List<Questiongroup> questiongroups) { 
    this.questiongroups = questiongroups; 
} 

public Responsedetail() { 
    super(); 
} 



class Questiongroup{ 

    @JsonProperty("questiongroup") 
    String questiongroup; 
    @JsonProperty("questions") 
    List<Question> questions; 

    public Questiongroup() { 
     super(); 
    } 

    public String getQuestiongroup() { 
     return questiongroup; 
    } 

    public void setQuestiongroup(String questiongroup) { 
     this.questiongroup = questiongroup; 
    } 

    public List<Question> getQuestions() { 
     return questions; 
    } 

    public void setQuestions(List<Question> questions) { 
     this.questions = questions; 
    } 
    class Question{ 

     @JsonProperty("question") 
     String question; 
     @JsonProperty("response") 
     String response; 
     public Question() { 
      super(); 
     } 
     public String getQuestion() { 
      return question; 
     } 
     public void setQuestion(String question) { 
      this.question = question; 
     } 
     public String getResponse() { 
      return response; 
     } 
     public void setResponse(String response) { 
      this.response = response; 
     }  
    } 
} 

}

コード変換

String jsonStrings ="{"questiongroups":[{"questiongroup":"1000","questions":[{"question":1001,"response":4},{"question":1002,"subquestion":1001,"response":"A2"}]}]}"; 

ObjectMapper mapper = new ObjectMapper(); 

    Responsedetail responsedetail = mapper.readValue(response.getDetails(), Responsedetail.class); 

    System.out.println(responsedetail); 
    System.out.println(responsedetail.questiongroups); 

答えて

2

あなたQuestiongroupクラスはResponsedetailクラス内にネストされ、そしてQuestionQuestiongroup内にネストされます。

staticにしていないので、inner classesです。

staticをネストされたクラスに追加して、の静的ネストされたクラスにします。

または、すべての最上位クラスを独自の.javaソースファイルにします。

+0

ありがとう、あなたは私を助け、私の時間を節約しました –

関連する問題