2012-02-23 91 views
1

我無法解析JSON字符串。下面的JSON給出:JSON - 對象數組內的對象數組

[ 
    { 
    "firstname": "firstname", 
    "lastname": "lastname", 
    "address": "address", 
    "images": [ 
     { 
     "image": {   
      "url": "url", 
      "id": "id" 
     } 
     }, 
     { 
     "image": { 
      "url": "url", 
      "id": "id" 
     } 
     } 
    ] 
    }, 
    { 
    "firstname": "firstname", 
    "lastname": "lastname", 
    "address": "address", 
    "images": [ 
     { 
     "image": {   
      "url": "url", 
      "id": "id" 
     } 
     }, 
     { 
     "image": { 
      "url": "url", 
      "id": "id" 
     } 
     } 
    ] 
    } 
] 

我已經定義所需的豆類如: Person.java

public class Person implements Serializable { 
    private static final long serialVersionUID = 38L; 

    private String firstname; 
    private String lastname; 
    private String address; 
    private Image[] images; 

    public Person() { 

    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public Image[] getImages() { 
     return images; 
    } 

    public void setImages(Image[] images) { 
     this.images = images; 
    } 
} 

Image.java:

public class Image implements Serializable { 
    private static final long serialVersionUID = 39L; 

    private String url; 
    private String id; 

    public Image() { 

    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    }  
} 

現在我解析JSON字符串作爲:

Gson gson = new Gson(); 
Person[] persons = (Person[])gson.fromJson(jsonString, Person[].class); 

現在如果我打印

System.out.println(persons[0].getFirstname()); 
System.out.println(persons[0].getLastname()); 
System.out.println(persons[0].getAddress()); 

它正在打印相應的值。另外:

persons[0].getImages() is not null; 
persons[0].getImages()[0] is not null; 

persons[0].getImages()[0].getUrl() is null; 
persons[0].getImages()[0].getId() is null; 

我無法明白我做錯了什麼?我在定義豆類時是否有任何錯誤?

我真的很感謝你的幫助。

回答

3

我認爲你不應該在數組內指定對象的名字。與單純

{   
    "url": "url", 
    "id": "id" 
} 

更換

"image": {   
    "url": "url", 
    "id": "id" 
} 

如果你想解析指定的JSON字符串你應該有differen對象結構:

public class Person implements Serializable { 
    private static final long serialVersionUID = 38L; 

    private String firstname; 
    private String lastname; 
    private String address; 
    private Foo[] images; 
} 

public class Foo{ 
    private Image image; 
} 
public class Image implements Serializable { 
    private static final long serialVersionUID = 39L; 

    private String url; 
    private String id; 
} 
+0

謝謝。你讓我今天一整天都感覺很好。 – 2012-02-23 14:02:03

1

我認爲這可能是你想你的JSON看起來更像

[ 
    { 
    "firstname": "firstname", 
    "lastname": "lastname", 
    "address": "address", 
    "images": [ 
     { 
      "url": "url", 
      "id": "id" 
     }... 

也就是說,它可能會得到的「圖像」標籤混淆了「圖片」內陣

+0

ks的答覆,但我不生成的JSON,我解析它。所以問題在於bean的定義!我該如何糾正它? – 2012-02-23 13:50:36

+0

我不確定。我認爲你需要在你的「圖像」對象中有一個「圖像」對象,但是這真是混亂和混亂的設計。希望有更多經驗的人可以向你展示一個很好的解決方法,但我的解決方案可能只是通過使用更精細的解析器的工作;例如[Jackson](http://jackson.codehaus.org/) 或者,當然,您可以在解析之前手動處理jsonstring。 – 2012-02-23 13:53:34

1

你需要另一個目的是包含圖像使得

class Image { 
    String image; Info info; 
} 

class Info { 
    String url, String id; 
}