需要JSON输出?

时间:2011-07-04 07:19:44

标签: java json

这里我在java中创建一个产品列表。我需要产品列表作为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

如何获得产品细节?请任何人帮助我。

3 个答案:

答案 0 :(得分:6)

我建议你看一下GSon。 =&GT; http://code.google.com/p/google-gson/

GSon是一个将Java对象转换为JSON对象的Java库。 ;)

我昨天发现了它。这很棒;快速高效。

答案 1 :(得分: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
    }
]

答案 2 :(得分:1)

有很多方法可以做到这一点,从简单的JSON对象库到支持双向映射的技术。

查看http://json.org以获取可供选择的列表。 (Java有21个条目...... 方式太多试图在这里总结。)