从 jsonobject 中删除行

remove line from jsonobject

我有一个这样的 JSON 对象, 它用于从 opencart API 获取运输方式。 我在这个 post how to fill array from json object 中问过 "how to fill array from this jsonobject in android" 之前的问题, 但我没有收到任何答复。

现在我想删除一些标有“====>”符号的行

{
  "shipping_methodsx": [
    {
      "title": "حمل و نقل با هزینه ثابت",
      "quote": [
        {
  ====>   "flat": {
            "code": "flat.flat",
            "title": "هزینه حمل و نقل",
            "cost": "10000",
            "tax_class_id": "0",
            "text": "10,000 تومان"
  ====>   }
        }
      ],
      "sort_order": "1",
      "error": false
    },
    {
      "title": "حمل و نقل رایگان",
      "quote": [
        {
  ====>   "free": {
            "code": "free.free",
            "title": "حمل و نقل رایگان",
            "cost": 0,
            "tax_class_id": 0,
            "text": "0 تومان"
  ====>   }
        }
      ],
      "sort_order": "3",
      "error": false
    },
    {
      "title": "پیک فروشگاه",
      "quote": [
        {
  ====>   "pickup": {
            "code": "pickup.pickup",
            "title": "ارسال توسط پیک فروشگاه",
            "cost": 0,
            "tax_class_id": 0,
            "text": "0 تومان"
   ====>  }
        }
      ],
      "sort_order": "2",
      "error": false
    }
  ]
}

并像这样在我的 android 应用程序中使用它:

{
  "shipping_methodsx": [
    {
      "title": "حمل و نقل با هزینه ثابت",
      "quote": [
        {
            "code": "flat.flat",
            "title": "هزینه حمل و نقل",
            "cost": "10000",
            "tax_class_id": "0",
            "text": "10,000 تومان"
        }
      ],
      "sort_order": "1",
      "error": false
    },
    {
      "title": "حمل و نقل رایگان",
      "quote": [
        {
            "code": "free.free",
            "title": "حمل و نقل رایگان",
            "cost": 0,
            "tax_class_id": 0,
            "text": "0 تومان"
        }
      ],
      "sort_order": "3",
      "error": false
    },
    {
      "title": "پیک فروشگاه",
      "quote": [
        {
            "code": "pickup.pickup",
            "title": "ارسال توسط پیک فروشگاه",
            "cost": 0,
            "tax_class_id": 0,
            "text": "0 تومان"
        }
      ],
      "sort_order": "2",
      "error": false
    }
  ]
}

谁能帮我做这个?

使用 gson 将 Json 字符串转换为 json 模型,然后更改 json 模型的内容以满足您的需要。最后,将 json 模型转换回 json 字符串。这里的示例代码:

public class Model {
    List<Model1> shipping_methodsx;

    class Model1 {
        String title;
        String sort_order;
        boolean error;

        List<Model2> quote;
        List<Model3> quote1;
    }

    class Model2 {
        @SerializedName(value = "flat", alternate = {"free", "pickup"})
        Model3 key;
    }

    class Model3 {
        String code;
        String title;
        String cost;
        String tax_class_id;
        String text;
    }
}

public static String removeSomeJson(String json) {
    Gson gson = new Gson();

    Model ret = gson.fromJson(json, Model.class);

    for(Model.Model1 model1: ret.shipping_methodsx){
        model1.quote1 = new ArrayList<>();
        for(Model.Model2 model2 : model1.quote) {
            model1.quote1.add(model2.key);
        }
        model1.quote = null;
    }

    return gson.toJson(ret);
}