0

我正在构建一个安静的 Web 服务以及一个使用 Web 服务的 Java 客户端。服务器发送给客户端的响应是 json 格式的。我想将json字符串中的属性和数据存储到一个数组中,这样我就可以使用这些数据来构建一些客户端操作。假设我有我有一系列产品。其余服务器的输出采用这种格式,我只是将它放在一个字符串变量中:

String output="{"product":[{"code":"7","name":"book","value":"4.0"},{"code":"3","name":"pen","value":"4.5"},{"code":"9","name":"pencil","value":"5.5"}]}"

我试图将 json 对象转换为 Product 类型的 java 对象

Gson gson = new GsonBuilder().serializeNulls().create();
Product product= gson.fromJson(output,Product.class);
System.out.println(product);

输出是:

product:[product:null,name:book,code:7,value:4,000000,       
product:null,name:pen,code:3,value:4,500000,  
product:null,name:pencil,code:9,value:5,500000],name:null,code:null,value:null

当我

System.out.println(product.product);

输出是:

[product:null,name:book,code:7,value:4,000000,     
product:null,name:pen,code:3,value:4,500000,   
product:null,name:pencil,code:9,value:5,500000]

第一个问题:我不知道为什么会出现空值,也找不到解决此问题的方法。

第二个问题:我已经搜索了几天,但我真的找不到将这些数据保存到数组或列表中的方法。我不想将它们保存在 java Products 对象中。我想要做的是将所有属性放在一起,例如所有值的数组,或者名称或所有产品。

Something like this I have in mind:

String[] names=["book","pen","pencil"];
Integer[] codes=[7,3,9];
Double[] values=[4.0,4.5,5.5];

第三个问题:我应该在服务器端进行这些操作(例如计算总值)吗?这会是更好的方法吗?

第四个问题:需要在Client中复制Products类吗?这就是我不确定 CRUD 之外的操作是否绝对应该在客户端的原因。

产品类别:

class Product  {

public String name;
public Integer code;
public Double value;
public List<Product> product;


public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name=name;
}

public Integer getCode()
{
    return code;
}

public void setCode(Integer code)
{
    this.code=code;
}

public Double getValue()
{
    return value;
}

public void setValue(Double value)
{
    this.value=value;
}
public List<Product> getProduct() 
{ 
    return product; 
}
 public void setProduct(List<Product> order) 
 { 
     this.product = product; 
 }
@Override
public String toString() {
 return    String.format("product:%s,name:%s,code:%d,value:%f",product,name,code,value);
      }
}

我正在使用 Gson 库,因为它似乎是最简单的方法。

4

1 回答 1

1

First, the appearance of the "null" values: That is probably due to the way your toString() method works. Just check what it does. If you append a null to a String, it will be printed as literal "null". Secondly, why don't you want to store your products as Product objects on the client? That's pretty much what object oriented programming is all about, keeping stuff together that belongs together. Just put them all in a Collection and define your methods for iterating over that Collection to get out all the prices, names and whatnot that you need or do whatever you need to do with your products. I see no good reason not to have the Product class in the client. Or rather use a shared project for client and server that contains the class, so you can use it in both wothout having to worry about keeping it in sync.

Your JSON String contains a list, but you try to retrieve a single object. Try this:

final List<Product> products = new Gson().fromJson(jsonString,
new TypeToken<Collection<Product>>() {
            }.getType());

Then you should be able to iterate over the result, e.g. like this:

double totalValue=0; 
for (Product product:products) {
    totalValue+=product.getValue();
}
于 2013-01-12T19:45:42.170 回答