2011-06-10 56 views
2

我試圖將JSON轉換爲GSON,但我無法建模。任何人都可以給我一個這個例子。Json to Gson - 建模

[ 
    { 
     "id": "1", 
     "name": "lalala", 
     "object1": [ 
      "string1", 
      "string1", 
      "string1" 
     ], 
     "object2": [ 
      "anotherString1", 
      "anotherString2" 
     ] 
    }, 
    { 
     "id": "2", 
     "name": "laaaaalala", 
     "object1": [ 
      "string1", 
      "string1", 
      "string1" 
     ], 
     "object2": [ 
      "anotherString1", 
      "anotherString2" 
     ] 
    } 
] 

感謝

回答

3

在這裏你去。

import java.io.FileReader; 
import java.util.List; 

import com.google.gson.Gson; 

public class Foo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    Gson gson = new Gson(); 
    Thing[] things = gson.fromJson(new FileReader("input.json"), Thing[].class); 
    System.out.println(gson.toJson(things)); 
    } 
} 

class Thing 
{ 
    String id; 
    String name; 
    String[] object1; 
    List<String> object2; 
}