代码之家  ›  专栏  ›  技术社区  ›  Alexa289

如何将GSON库中的JSON字符串转换为JSON对象?

  •  0
  • Alexa289  · 技术社区  · 7 年前

    我知道有些线程有类似的问题,但我找不到解决方案。

    我是移动开发的初学者。我有点困惑如何转换 JSON string 恢复正常 JSON .

    所以我有 JSON 像 一串 就像下面这个 json 一串 是由 GSON 那个皈依者 Java 反对 JSON 一串 :

    产品=%5B%7B%22客户篮子_日期_添加%22%3A%2212019-04-22%2012%3A28%3A41%22%2C%22客户篮子_id%22%3A4%2C%22客户篮子_产品%22%3A%7B%22属性%3A%5B%5D%2C%22属性_价格%22%3A%220.0%22%2C%22类别_id%22%3A8%2C%22类别_名称%22%3A%22b%22b%22b%22b%22b%22b%kemasan%20Botol%22%2C%22%22%客户篮子_数量%225;%225;%22%22%22%22%2%2%2%2%2%2%2%2%2%22%22%22%22%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%22%22%22%22%22%22%22%22%22%22%22%22%22%22%22%22%22%22%2%22%2%22%22%2%22%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%汽车驾驶者离子%22%3A%22促销%20APRIL%20Urah%21%5Cu003cbr%5Cu003e%5Cr%5CnRekomendasi%20harga%20jual%20warung%20Rp%203.500%2Botol%5Cu003cbr%5Cu003e%5Cr%5Cnuntuntung%20Rp%201.300%20%2859%25%29%5Cu003cbr%5Cu003e%5Cr%20Cr%20min%20Cr%20min%20botol%2C%20maks%2012%20botol%22%22%22Cr%22最终价格%22%22Cr%22%22%22产品%22%22Cr%22%22%资源%2资产%2图像%2产品图像%2F155572222。宣传拉鲁坦·卡基蒂加。jpg%22%2C%22产品\u喜欢%22%3A0%2C%22产品\u型号%22%3A%22%2C%22产品\u名称%22%3A%22促销%20Cap%20Kaki%20Tiga%20Botol%20200%20ml%22%2C%22订购产品%22%3A0%2C%22产品\u价格%22%3A%222200.0%22%2C%22产品\u数量%22%3A1%2C%22产品\u状态%22%22%3A0%2C%22产品\u税务等级\u22%22%22%3A%221%22%2C%22产品重量单位%22%3A%22Btl%22%2C%22分拣订单%22%3A0%2C%22税种id%22%3A0%2C%22税种优先级%22%3A0%2C%22税种id%22%3A0%2C%22税区id%22%3A0%2C%22总价格%22%3A%2213200%22%7D%2C%22%22%22%22%22%22%22%5D%22%22%22%5D%22%5D%22%5D;状态=0

    这个JSON字符串是从

    new Gson().toJson(cartItemsList)
    

    而且 cartItemsList 是类的一个实例 CartProduct

    public class CartProduct {
    
    
        @SerializedName("customers_id")
        @Expose
        private int customersId;
        @SerializedName("customers_basket_id")
        @Expose
        private int customersBasketId;
        @SerializedName("customers_basket_date_added")
        @Expose
        private String customersBasketDateAdded;
        @SerializedName("customers_basket_product")
        @Expose
        private ProductDetails customersBasketProduct;
        @SerializedName("customers_basket_product_attributes")
        @Expose
        private List<CartProductAttributes> customersBasketProductAttributes = new ArrayList<CartProductAttributes>();
    
    
    
        public int getCustomersId() {
            return customersId;
        }
    
        public void setCustomersId(int customersId) {
            this.customersId = customersId;
        }
    
        public int getCustomersBasketId() {
            return customersBasketId;
        }
    
        public void setCustomersBasketId(int customersBasketId) {
            this.customersBasketId = customersBasketId;
        }
    
        public String getCustomersBasketDateAdded() {
            return customersBasketDateAdded;
        }
    
        public void setCustomersBasketDateAdded(String customersBasketDateAdded) {
            this.customersBasketDateAdded = customersBasketDateAdded;
        }
    
        public ProductDetails getCustomersBasketProduct() {
            return customersBasketProduct;
        }
    
        public void setCustomersBasketProduct(ProductDetails customersBasketProduct) {
            this.customersBasketProduct = customersBasketProduct;
        }
    
        public List<CartProductAttributes> getCustomersBasketProductAttributes() {
            return customersBasketProductAttributes;
        }
    
        public void setCustomersBasketProductAttributes(List<CartProductAttributes> customersBasketProductAttributes) {
            this.customersBasketProductAttributes = customersBasketProductAttributes;
        }
    
    }
    

    还有这个班 CartProductAttributes

    public class CartProductAttributes implements Parcelable {
    
        @SerializedName("customers_basket_id")
        @Expose
        private int customersBasketId;
        @SerializedName("products_id")
        @Expose
        private String productsId;
        @SerializedName("option")
        @Expose
        private Option option;
        @SerializedName("values")
        @Expose
        private List<Value> values = new ArrayList<Value>();
    
    
        public CartProductAttributes() {
        }
    
    
        public int getCustomersBasketId() {
            return customersBasketId;
        }
    
        public void setCustomersBasketId(int customersBasketId) {
            this.customersBasketId = customersBasketId;
        }
    
        public String getProductsId() {
            return productsId;
        }
    
        public void setProductsId(String productsId) {
            this.productsId = productsId;
        }
    
        /**
         *
         * @return
         *     The option
         */
        public Option getOption() {
            return option;
        }
    
        /**
         *
         * @param option
         *     The option
         */
        public void setOption(Option option) {
            this.option = option;
        }
    
        /**
         *
         * @return
         *     The values
         */
        public List<Value> getValues() {
            return values;
        }
    
        /**
         *
         * @param values
         *     The values
         */
        public void setValues(List<Value> values) {
            this.values = values;
        }
    
    
    
        //********** Describes the kinds of Special Objects contained in this Parcelable Instance's marshaled representation *********//
    
        @Override
        public int describeContents() {
            return 0;
        }
    
    
    
        //********** Writes the values to the Parcel *********//
    
        public void writeToParcel(Parcel parcel_out, int flags) {
            parcel_out.writeValue(customersBasketId);
            parcel_out.writeValue(productsId);
            parcel_out.writeParcelable(option, flags);
            parcel_out.writeList(values);
        }
    
    
    
        //********** Generates Instances of Parcelable class from a Parcel *********//
    
        public static final Creator<CartProductAttributes> CREATOR = new Creator<CartProductAttributes>() {
    
            // Creates a new Instance of the Parcelable class, Instantiating it from the given Parcel
            @Override
            public CartProductAttributes createFromParcel(Parcel parcel_in) {
                return new CartProductAttributes(parcel_in);
            }
    
            // Creates a new array of the Parcelable class
            @Override
            public CartProductAttributes[] newArray(int size) {
                return new CartProductAttributes[size];
            }
        };
    
    
    
        //********** Retrieves the values from the Parcel *********//
    
        protected CartProductAttributes(Parcel parcel_in) {
            this.customersBasketId = parcel_in.readInt();
            this.productsId = parcel_in.readString();
    
            this.option = parcel_in.readParcelable(Option.class.getClassLoader());
    
            this.values = new ArrayList<Value>();
            parcel_in.readList(values, Value.class.getClassLoader());
        }
    
    }
    

    下面是ProductDetails类

    public class ProductDetails implements Parcelable {
    
    
        @SerializedName("products_id")
        @Expose
        private int productsId;
        @SerializedName("products_quantity")
        @Expose
        private int productsQuantity;
        @SerializedName("products_model")
        @Expose
        private String productsModel;
        @SerializedName("products_image")
        @Expose
        private String productsImage;
        @SerializedName("products_price")
        @Expose
        private String productsPrice;
        @SerializedName("discount_price")
        @Expose
        private String discountPrice;
        @SerializedName("products_date_added")
        @Expose
        private String productsDateAdded;
        @SerializedName("products_last_modified")
        @Expose
        private String productsLastModified;
        @SerializedName("products_date_available")
        @Expose
        private String productsDateAvailable;
        @SerializedName("products_weight")
        @Expose
        private String productsWeight;
        @SerializedName("products_weight_unit")
        @Expose
        private String productsWeightUnit;
        @SerializedName("products_status")
        @Expose
        private int productsStatus;
        @SerializedName("products_ordered")
        @Expose
        private int productsOrdered;
        @SerializedName("products_liked")
        @Expose
        private int productsLiked;
        @SerializedName("language_id")
        @Expose
        private int languageId;
        @SerializedName("products_name")
        @Expose
        private String productsName;
        @SerializedName("products_description")
        @Expose
        private String productsDescription;
        @SerializedName("products_url")
        @Expose
        private String productsUrl;
        @SerializedName("products_viewed")
        @Expose
        private int productsViewed;
        @SerializedName("products_tax_class_id")
        @Expose
        private int productsTaxClassId;
        @SerializedName("tax_rates_id")
        @Expose
        private int taxRatesId;
        @SerializedName("tax_zone_id")
        @Expose
        private int taxZoneId;
        @SerializedName("tax_class_id")
        @Expose
        private int taxClassId;
        @SerializedName("tax_priority")
        @Expose
        private int taxPriority;
        @SerializedName("tax_rate")
        @Expose
        private String taxRate;
        @SerializedName("tax_description")
        @Expose
        private String taxDescription;
        @SerializedName("tax_class_title")
        @Expose
        private String taxClassTitle;
        @SerializedName("tax_class_description")
        @Expose
        private String taxClassDescription;
        @SerializedName("categories_id")
        @Expose
        private int categoriesId;
        @SerializedName("categories_name")
        @Expose
        private String categoriesName;
        @SerializedName("categories_image")
        @Expose
        private String categoriesImage;
        @SerializedName("categories_icon")
        @Expose
        private String categoriesIcon;
        @SerializedName("parent_id")
        @Expose
        private int parentId;
        @SerializedName("sort_order")
        @Expose
        private int sortOrder;
        @SerializedName("isLiked")
        @Expose
        private String isLiked;
        @SerializedName("manufacturers_id")
        @Expose
        private int manufacturersId;
        @SerializedName("manufacturers_name")
        @Expose
        private String manufacturersName;
        @SerializedName("manufacturers_image")
        @Expose
        private String manufacturersImage;
        @SerializedName("manufacturers_url")
        @Expose
        private String manufacturersUrl;
        @SerializedName("date_added")
        @Expose
        private String dateAdded;
        @SerializedName("last_modified")
        @Expose
        private String lastModified;
    
        @SerializedName("isSale_product")
        @Expose
        private String isSaleProduct;
    
    
        @SerializedName("attributes_price")
        @Expose
        private String attributesPrice;
        @SerializedName("final_price")
        @Expose
        private String productsFinalPrice;
        @SerializedName("total_price")
        @Expose
        private String totalPrice = "";
    
        @SerializedName("customers_basket_quantity")
        @Expose
        private int customersBasketQuantity;
    
        @SerializedName("images")
        @Expose
        private List<Image> images = new ArrayList<Image>();
    
        @SerializedName("attributes")
        @Expose
        private List<Attribute> attributes = new ArrayList<Attribute>();
    
        @SerializedName("motorist_id")
        @Expose
        private long mMotoristId = 0;
    
        @SerializedName("motorist_name")
        @Expose
        private String mMotoristName = "";
    
        @SerializedName("qty_orders_min")
        @Expose
        private int mQuantityMin = 0;
    
        @SerializedName("qty_orders_max")
        @Expose
        private int mQuantityMax = 0;
    
        @SerializedName("id_jenis_products")
        @Expose
        private int mProductType = 0;
    
        @SerializedName("trans_limit")
        @Expose
        private int mQuantityPromoLimit = 0;
    
        @SerializedName("trans_live")
        @Expose
        private int mQuantityPromoLive = 0;
    
        @SerializedName("qty_promo_bought")
        @Expose
        private int mQuantityPromoBought = 0;
    
    
        @Override
        public String toString() {
            StringBuilder tempToString = new StringBuilder("\n--- ProductDetails ---\n");
            tempToString.append("productsId: ").append(productsId).append("\n");
            tempToString.append("productsName: ").append(productsName).append("\n");
            tempToString.append("categoriesId: ").append(categoriesId).append("\n");
            tempToString.append("categoriesName: ").append(categoriesName).append("\n");
            tempToString.append("productsQuantity: ").append(productsQuantity).append("\n");
            tempToString.append("productsDateAdded: ").append(productsDateAdded).append("\n");
            tempToString.append("totalPrice: ").append(totalPrice).append("\n");
            tempToString.append("minQuantity: ").append(mQuantityMin).append("\n");
            tempToString.append("maxQuantity: ").append(mQuantityMax).append("\n");
            tempToString.append("mProductType: ").append(mProductType).append("\n");
            tempToString.append("mQuantityPromoLimit: ").append(mQuantityPromoLimit).append("\n");
            tempToString.append("mQuantityPromoLive: ").append(mQuantityPromoLive).append("\n");
            tempToString.append("mQuantityPromoBought: ").append(mQuantityPromoBought).append("\n");
            return tempToString.toString();
        }
    
    
    }
    

    我想把这个字符串JSON转换成真正的JSON,例如:

    {
        "type": "donut",
        "name": "Cake",
        "toppings": [
            { "id": "5002", "type": "Glazed" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5004", "type": "Maple" }
        ]
    }
    

    所以我可以阅读并复制它 JSON ,并将其作为 JSON 使用swift编程语言从iOS设备到服务器的字符串。因为我需要把它贴出来 JSON 请求正文中的字符串,如下所示:

    enter image description here

    我明白了 JSON 一串 来自我们的android开发者。这 JSON 一串 是由 格森 Android中的库,并将Java对象转换为 JSON 像那样系绳子。不幸的是,android开发者也不知道如何将其转换为现实 JSON 不 JSON 一串 .

    0 回复  |  直到 7 年前