2015-05-21 136 views
-7

我應該準備什麼java類結構來返回這樣的JSON?將JSON轉換爲java對象

修正JSON(上面一個無效):

{ 
    "transactionComparisonTotals": [ 
     [ 
      "CurrentFace", 
      { 
       "value": "1000000", 
       "format": "$000.00 ptr" 
      }, 
      { 
       "value": "1000", 
       "format": "$000.00 ptr" 
      }, 
      { 
       "value": "0", 
       "format": "$000.00 ptr" 
      } 
     ], 
     [ 
      "MarketPrincipal", 
      { 
       "value": "1000000", 
       "format": "$000.00 ptr" 
      }, 
      { 
       "value": "1000", 
       "format": "$000.00 ptr" 
      }, 
      { 
       "value": "0", 
       "format": "$000.00 ptr" 
      } 
     ] 
    ] 
} 

爲此,我需要設置的Java類。 Ø

這麼一件事我可以做的是產生JSON,如:

{ 
     "transactionComparisonTotals": [ 
      { 
       "key": "coupon", 
       "valueAttributes": [ 
        { 
         "value": 4.25, 
         "format": "00.00%", 
         "color": true, 
         "sign": true 
        }, 
        { 
         "value": 4.26, 
         "format": "$00.00 %", 
         "color": true, 
         "sign": true 
        }, 
        { 
         "value": 0.31, 
         "format": "00.00 bp", 
         "color": true, 
         "sign": true 
        } 
       ] 
      } 
} 

但我真正想要的是有「重點」和「valueAttributes」在短短一個數組沒有財產(如所示我原始的JSON)。

+0

JSON的不是一個有效的。 – shruti1810

+0

@JackSparrow調查GSON庫來解析兒子,這非常簡單。 – StackFlowed

+0

我其實想要返回這樣的迴應,所以爲此我確實需要建立java類層次結構嗎? – JackSparrow

回答

2

考慮到這個json文件transaction.json :(你的是無效的,所以我試圖糾正它只是爲了得到使用gson谷歌API的序列化和deserilization的想法)。

{ 
    "transactionComparisonTotals": [ 
     { 
      "name": "CurrentFace", 
      "info": 
      [ 
       { 
        "value": "1000000", 
        "format": "$000.00 ptr" 
       }, 
       { 
        "value": "1000", 
        "format": "$000.00 ptr" 
       }, 
       { 
        "value": "0", 
        "format": "$000.00 ptr" 
       } 
      ] 
     }, 
     { 
      "name": "MarketPrincipal", 
      "info": 
      [ 
       { 
        "value": "1000000", 
        "format": "$000.00 ptr" 
       }, 
       { 
        "value": "1000", 
        "format": "$000.00 ptr" 
       }, 
       { 
        "value": "0", 
        "format": "$000.00 ptr" 
       } 
      ] 
     } 
    ] 
} 

創建這些類:

數據類:

public class Data{ 
    List<TransactionComparisonTotal> transactionComparisonTotals; 

    public List<TransactionComparisonTotal> getTransactionComparisonTotals() { 
     return transactionComparisonTotals; 
    } 

    public void setTransactionComparisonTotals(
      List<TransactionComparisonTotal> transactionComparisonTotals) { 
     this.transactionComparisonTotals = transactionComparisonTotals; 
    } 

    @Override 
    public String toString() { 
     return transactionComparisonTotals.toString(); 
    } 

} 

TransactionComparisonTotal類:

public class TransactionComparisonTotal{ 
    String name; 
    List<Info> info; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<Info> getInfo() { 
     return info; 
    } 
    public void setInfo(List<Info> info) { 
     this.info = info; 
    } 
    @Override 
    public String toString() { 
     return "\n"+name+","+info.toString()+"\n"; 
    } 

} 

信息類:

public class Info{ 
    String value; 
    String format; 
    public String getValue() { 
     return value; 
    } 
    public void setValue(String value) { 
     this.value = value; 
    } 
    public String getFormat() { 
     return format; 
    } 
    public void setFormat(String format) { 
     this.format = format; 
    } 
    @Override 
    public String toString() { 
     return value+","+format; 
    } 

} 

這是使用GSON谷歌API

public class Transaction { 
    public static void main(String[] args) throws JsonIOException, JsonSyntaxException, FileNotFoundException { 
     Gson gson = new Gson(); 
     Data data = gson.fromJson(new BufferedReader(new FileReader(
       "transaction.json")), new TypeToken<Data>() { 
     }.getType()); 
     System.out.println(data); 
    } 
} 

輸出deserilization的一個簡單的例子:

[ 
CurrentFace,[1000000,$000.00 ptr, 1000,$000.00 ptr, 0,$000.00 ptr] 
, 
MarketPrincipal,[1000000,$000.00 ptr, 1000,$000.00 ptr, 0,$000.00 ptr] 
]