2012-01-17 4 views
0

こんにちは、私はこのJSON文字列を呼び出していますWebサービスからの応答として受け付けておりますみんな、JavaでGSONを使用したWebサービスの応答から得られた複雑なJSON文字列を解析する方法は?

 I am new to Json/Gson. I have already looked all around the internet for help, but I  have seen nothing so similar to what I need to parse. So I am posting here, any help will be appreciated. 

 Json String = {"courses":[{"links":   [{"href":"https://xp.student.com/courses/6364145","rel":"self","title":"course"}]},   {"links": [{"href":"https://xp.student.com/courses/6364143","rel":"self","title":"course"}]}, 
     {"links": [{"href":"https://xp.student.com/courses/6364144","rel":"self","title":"course"}]}]} 

私はすでに私は「JSONを取得する時点までのコードを行っています文字列 ":

InputStreamReader reader = new InputStreamReader(httpConn.getInputStream()); 
     in = new BufferedReader(reader); 
    Gson gson = new Gson(); 
    Course courses = new Gson().fromJson(in,Course.class); 

は、私はまた、以下のクラスを作成しました

 import ccSample.Type.Course.Link; 

     public class Course { 

     public Link links[]; 

     } 

     public class Link{ 
     public String href; 
     public String rel; 
     public String title; 
     public String getHref() { 
     return href; 
     } 
     public void setHref(String href) { 
     this.href = href; 
     } 
     public String getRel() { 
     return rel; 
     } 
     public void setRel(String rel) { 
     this.rel = rel; 
     } 
     public String getTitle() { 
     return title; 
     } 
     public void setTitle(String title) { 
     this.title = title; 
     } 

     } 





     but I am just getting a null courses object and do not know what I am missing,  any suggestions corrections are welcome! 

    Thank you very much :) 

答えて

0

これはタマネギの層です。一度に1つずつ剥がしてください。 instanceof演算子および/またはgetClass().getName()メソッド呼び出しを使用して、各ステップでの処理内容を把握し、期待どおりに再確認することができます。

0

ああ、落とし穴が...

あなたは他のJSONオブジェクトに含まれているその応答で以来、コース別のラッパーが必要

public class Reply { 
    private Course[] courses; 

    public void setCourses(Course[] courses) { 
     this.courses = courses; 
    } 

    public Course[] getCourses() { 
     return courses; 
    } 
} 

そして

Reply reply = new Gson().fromJson(in,Reply.class);

を行う必要がありますそれ:

関連する問題