2011-07-04 83 views
2

在這裏我創建一個產品列表在Java中。我需要產品列表作爲JSON。這個怎麼做 ?需要JSON輸出嗎?

private List<ProductFromServiceTemp> getListFromWebservice() 
    { 

     List<ProductFromServiceTemp> productList = new ArrayList<ProductFromServiceTemp>(); 
     for(int i = 0; i < 10; i++) 
     { 
      ProductFromServiceTemp product = new ProductFromServiceTemp(); 
      product.setName("name " + i); 
      product.setDescription("desc " + i); 
      product.setPrice(i * 100d); 
      productList.add(product); 
      System.out.println("Product = " + product); 
      System.out.println("productList = " + productList); 
     } 

     return productList; 
    } 

所以現在像這樣

`Array ar = [prod1, prod2];` 

然後

ar[i].name 
ar[i].price 

如何獲得產品的細節?請任何人幫助我。

+0

您是否使用特定的JSON庫卡住了,還是您可以使用任何您認爲合適的庫? –

+1

你的問題是模糊的。你使用哪種JSON技術?那裏有很多圖書館。 – drekka

+0

我想將java中的產品列表解析爲JSON?說怎麼解析? – selladurai

回答

6

我建議你看看GSon。 =>http://code.google.com/p/google-gson/

GSon是一個將Java對象轉換爲JSON對象的Java庫。 ;)

我昨天發現了它。這很棒;快速和高效。

+1

+1我最近將gson整合到了我的項目中......它很有趣 – Bohemian

2

如果你想選擇任何你喜歡的JSON庫,Jackson是一個不錯的選擇。

以下是傑克遜在行動中的一個演示,以解決最初提出的問題。

import java.util.ArrayList; 
import java.util.List; 

import org.codehaus.jackson.map.ObjectMapper; 

public class JacksonDemo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    ObjectMapper mapper = new ObjectMapper(); 
    List<ProductFromServiceTemp> products = getListFromWebservice(); 
    String json = mapper.writeValueAsString(products); 
    System.out.println(json); 
    } 

    private static List<ProductFromServiceTemp> getListFromWebservice() 
    { 
    List<ProductFromServiceTemp> productList = new ArrayList<ProductFromServiceTemp>(); 
    for (int i = 0; i < 10; i++) 
    { 
     ProductFromServiceTemp product = new ProductFromServiceTemp(); 
     product.setName("name " + i); 
     product.setDescription("desc " + i); 
     product.setPrice(i * 100d); 
     productList.add(product); 
    } 
    return productList; 
    } 
} 

class ProductFromServiceTemp 
{ 
    private String name; 
    private String description; 
    private double price; // Don't use double type for financial information. 

    public String getName() {return name;} 
    public void setName(String name) {this.name = name;} 
    public String getDescription() {return description;} 
    public void setDescription(String description) {this.description = description;} 
    public double getPrice() {return price;} 
    public void setPrice(double price) {this.price = price;} 
} 

的JSON輸出:

[ 
    { 
     "name": "name 0", 
     "description": "desc 0", 
     "price": 0 
    }, 
    { 
     "name": "name 1", 
     "description": "desc 1", 
     "price": 100 
    }, 
    { 
     "name": "name 2", 
     "description": "desc 2", 
     "price": 200 
    }, 
    { 
     "name": "name 3", 
     "description": "desc 3", 
     "price": 300 
    }, 
    { 
     "name": "name 4", 
     "description": "desc 4", 
     "price": 400 
    }, 
    { 
     "name": "name 5", 
     "description": "desc 5", 
     "price": 500 
    }, 
    { 
     "name": "name 6", 
     "description": "desc 6", 
     "price": 600 
    }, 
    { 
     "name": "name 7", 
     "description": "desc 7", 
     "price": 700 
    }, 
    { 
     "name": "name 8", 
     "description": "desc 8", 
     "price": 800 
    }, 
    { 
     "name": "name 9", 
     "description": "desc 9", 
     "price": 900 
    } 
] 
1

有很多方法可以做到這一點,從簡單的JSON對象庫,以支持雙向映射技術。

查看http://json.org瞭解可用選項列表。 (有21條Java ...這是方式太多這裏試着總結一下。)