1

用户可以将商品添加到购物车,同时会触发一个 url 并返回一个 json..用户可以添加任意数量的产品..每个产品都有一个唯一的 product_id。现在每个产品可以有很多供应商..供应商也有唯一的供应商 ID ..对于添加到购物车的所有项目供应商可能很少见。假设我们有 5 个产品放入购物车,..suppllier1(具有唯一 ID)提供 3 个产品,供应商2(具有唯一 ID)提供 2 个产品..现在我有发现并在数组列表中添加了唯一的供应商 ID ..在每个 json 对象中都有一个名为价格的字段..我必须添加一个唯一供应商的价格..所以对于 2 个供应商,将显示 2 个单独的价格..(第一个供应商 1 通过添加他提供的 3 种产品的价格)和其他供应商 2 的价格 .. json 的一部分是

   {
      "BasketItemID": "4455",
      "BasketID": "11",
      "ProductID": "12909",
      "Qty": "1",
      "SupplierID": "7",
      "Code": "END10-001",
      "Name": "ENDO STOPPERS (100)",

      "Price": "5.72",
      "GST": "0.64",
      "PriceGST": "6.36",
      "Brand": "DENT AMERICA",
      "Thumbnail": null,

    },
    {
      "BasketItemID": "4464",
      "BasketID": "11",
      "ProductID": "12914",
      "Qty": "1",
      "SupplierID": "7",
      "Code": "INS52-361",
      "Name": "AMALGAM GUN CVD 45' PLASTIC",

      "Price": "17.00",
      "GST": "1.70",
      "PriceGST": "18.70",
      "Brand": "LARIDENT",
      "Thumbnail": null,

    },
    {
      "BasketItemID": "4465",
      "BasketID": "11",
      "ProductID": "13863",
      "Qty": "1",
      "SupplierID": "5",
      "Code": "9516068",
      "Name": "Tofflemire Bands #3 0015 Pkt 12",

      "Price": "2.24",
      "GST": "0.22",
      "PriceGST": "2.47",
      "Brand": "Rand",
      "Thumbnail": null,

    },

那么如何添加价格?

4

1 回答 1

0

If your problem revolves around parsing a JSON array you may want to check out the example here. Then you would filter by supplier, feel free to let me expand if you want to make your requirements more explicit.

It would look something like this (not tested)

Integer id;
Map<Integer, Double> sums;
...
while (reader.hasNext()) {
   String name = reader.nextName();
   if (name.equals("SupplierID")) {
     id = reader.nextInt();
   } else if (name.equals("Price")) {
     if(!sums.containsKey(id)){
        sums.put(id, reader.nextDouble());
     }
     else{
       Double f = (Double) sums.get(id);
       sums.put(id, f+reader.nextDouble());
     }
   } else {
     reader.skipValue();
   }
}
于 2013-07-18T12:52:32.197 回答