【问题标题】:I want to create a json structure like this with the gson library with the proper object creation我想用 gson 库创建一个像这样的 json 结构,并创建正确的对象
【发布时间】:2017-11-17 06:27:16
【问题描述】:
{
   "restaurantId":"0a60fa5e-642c-4e4f-ae37-a7c8093e1659",
   "restaurantName":"Food Market",
   "paymentRefKey":"cpmqmvpv",
   "paymentStatus":"success",
   "paymentMode":"paypal_account",
   "transactionStatus":"settling",
   "payerId":"PAYER_ID-2f3c6088eb828dab",
   "paypalPaymentId":"PAY-e43a2913d8ad92ccda1cf872",
   "orderNetAmount":"21.825",
   "salesTax":0,
   "serviceFee":1.575,
   "discount":0,
   "subTotal":22.5,
   "orderQuantity":1,
   "userId":"68503c6e-9475-4081-8bdf-199bc194745f",
   "firstName":"test",
   "lastName":"customer",
   "email":"test.customer@yopmail.com",
   "contactNumber":"9516238470",
   "couponcode":{
      "type":"percent",
      "midval":2.25,
      "code":"QDH596"
   },
   "orderLines":[
      {
         "quantity":1,
         "offerId":"2bb21254-fe7d-4695-ae99-d420fdae3aca",
         "startValidity":"2017-09-26T06:55:00.000Z",
         "endValidity":"2017-11-02T01:00:00.000Z",
         "categoryId":"dbae9249-705a-4d7a-86ce-6f5c5c180855",
         "itemName":"Dosa.",
         "menuId":"02bfcaec-0bb1-42b9-87a5-c60e04fcf9fc",
         "actualPrice":25,
         "dealPrice":22.5,
         "netAmount":22.5,
         "discountPercent":10,
         "type":"offer",
         "itemImagePath":"offers/offer_1506408444171.jpeg",
         "unlimitedQuantity":true
      }
   ]
}

这是我想通过在android 中使用gson 制作的json 甲酸盐我已经制作了pojo 类,但我无法生成相同的甲酸盐 我使用gson 生成甲酸盐,如下所示

[
   {
      "contactNumber":"8149425758",
      "couponcode":{
         "midval":0,
         "type":"percent"
      },
      "discount":0,
      "email":"udasibharat1@gmail.com",
      "firstName":"Bharat",
      "lastName":"Udasi",
      "orderLines":[
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8",
            "quantity":3,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":false
         },
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199",
            "quantity":11,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":80,
            "dealPrice":80,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Burger_1507295095917.jpeg",
            "itemName":"Burger",
            "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3",
            "netAmount":80,
            "quantity":11,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":50,
            "dealPrice":50,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg",
            "itemName":"Panir Tikka",
            "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f",
            "netAmount":50,
            "quantity":9,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         }
      ],
      "orderNetAmount":"24.0",
      "orderQuantity":34,
      "payerId":"60g59yxg",
      "paymentMode":"paypal_account",
      "paymentRefKey":"60g59yxg",
      "paymentStatus":"1000",
      "paypalPaymentId":"60g59yxg",
      "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a",
      "restaurantName":"Michel Plaza",
      "salesTax":0,
      "serviceFee":156.8,
      "subTotal":1960,
      "transactionStatus":"settling",
      "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1"
   },
   {
      "contactNumber":"8149425758",
      "couponcode":{
         "midval":0,
         "type":"percent"
      },
      "discount":0,
      "email":"udasibharat1@gmail.com",
      "firstName":"Bharat",
      "lastName":"Udasi",
      "orderLines":[
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8",
            "quantity":3,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":false
         },
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199",
            "quantity":11,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":80,
            "dealPrice":80,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Burger_1507295095917.jpeg",
            "itemName":"Burger",
            "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3",
            "netAmount":80,
            "quantity":11,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":50,
            "dealPrice":50,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg",
            "itemName":"Panir Tikka",
            "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f",
            "netAmount":50,
            "quantity":9,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         }
      ],
      "orderNetAmount":"24.0",
      "orderQuantity":34,
      "payerId":"60g59yxg",
      "paymentMode":"paypal_account",
      "paymentRefKey":"60g59yxg",
      "paymentStatus":"1000",
      "paypalPaymentId":"60g59yxg",
      "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a",
      "restaurantName":"Michel Plaza",
      "salesTax":0,
      "serviceFee":156.8,
      "subTotal":1960,
      "transactionStatus":"settling",
      "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1"
   },
   {
      "contactNumber":"8149425758",
      "couponcode":{
         "midval":0,
         "type":"percent"
      },
      "discount":0,
      "email":"udasibharat1@gmail.com",
      "firstName":"Bharat",
      "lastName":"Udasi",
      "orderLines":[
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8",
            "quantity":3,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":false
         },
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199",
            "quantity":11,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":80,
            "dealPrice":80,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Burger_1507295095917.jpeg",
            "itemName":"Burger",
            "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3",
            "netAmount":80,
            "quantity":11,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":50,
            "dealPrice":50,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg",
            "itemName":"Panir Tikka",
            "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f",
            "netAmount":50,
            "quantity":9,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         }
      ],
      "orderNetAmount":"24.0",
      "orderQuantity":34,
      "payerId":"60g59yxg",
      "paymentMode":"paypal_account",
      "paymentRefKey":"60g59yxg",
      "paymentStatus":"1000",
      "paypalPaymentId":"60g59yxg",
      "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a",
      "restaurantName":"Michel Plaza",
      "salesTax":0,
      "serviceFee":156.8,
      "subTotal":1960,
      "transactionStatus":"settling",
      "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1"
   },
   {
      "contactNumber":"8149425758",
      "couponcode":{
         "midval":0,
         "type":"percent"
      },
      "discount":0,
      "email":"udasibharat1@gmail.com",
      "firstName":"Bharat",
      "lastName":"Udasi",
      "orderLines":[
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8",
            "quantity":3,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":false
         },
         {
            "actualPrice":50,
            "dealPrice":45,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png",
            "itemName":"Panir Tikka",
            "netAmount":45,
            "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199",
            "quantity":11,
            "startValidity":"1970-01-01T01:30:00.000Z",
            "type":"offer",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":80,
            "dealPrice":80,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Burger_1507295095917.jpeg",
            "itemName":"Burger",
            "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3",
            "netAmount":80,
            "quantity":11,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         },
         {
            "actualPrice":50,
            "`enter code here`dealPrice":50,
            "discountPercent":0,
            "endValidity":"1970-01-01T17:30:00.000Z",
            "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg",
            "itemName":"Panir Tikka",
            "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f",
            "netAmount":50,
            "quantity":9,
            "startValidity":"1970-01-01T17:30:00.000Z",
            "type":"menu",
            "unlimitedQuantity":true
         }
      ],
      "orderNetAmount":"24.0",
      "orderQuantity":34,
      "payerId":"60g59yxg",
      "paymentMode":"paypal_account",
      "paymentRefKey":"60g59yxg",
      "paymentStatus":"1000",
      "paypalPaymentId":"60g59yxg",
      "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a",
      "restaurantName":"Michel Plaza",
      "salesTax":0,
      "serviceFee":156.8,
      "subTotal":1960,
      "transactionStatus":"settling",
      "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1"
   }
]     

【问题讨论】:

  • 我看不出你的实际结果有什么问题。你能解释什么是错的吗?
  • 我需要以对象形式的结果以数组形式出现,这意味着看到 json 以 [ 开头,但我需要以 { 开头。为此我需要帮助

标签: android json parsing serialization gson


【解决方案1】:

你已经完成了所有的部分。您需要做的最后一件事是将 gson 字符串转换为 JSONArray。使用此代码

JSONArray jsonarray = new JSONArray("gson string");

如果你需要这样

{
   "restaurantId":"0a60fa5e-642c-4e4f-ae37-a7c8093e1659",
      '
      '
      '
      '
      '
   "contactNumber":"9516238470",
   "couponcode":{
      '
      '
      '
      '
   },
   "orderLines":[
      '
      '
      '
      '
   ]
}

不是这样的

{
   "contactNumber":"8149425758",
      '
      '
      '
      '
      '
   "couponcode":{
      '
      '
      '
      '
   },
   "lastName":"Udasi",
   "orderLines":[
      '
      '
      '
      '
   ],
   "orderNetAmount":"24.0",
      '
      '
      '
}

这是因为数据按元素键排序。查看元素键然后你可以看到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2015-05-06
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多