以编程方式构造JSON数组时,从JSON格式中删除反斜杠

时间:2018-12-26 11:23:49

标签: java android json

我想通过从JSON格式中删除反斜杠来解析JSON数据。

我尝试通过以下代码从JSON格式中删除反斜杠,但没有成功

      JSONArray packsJSON  = new JSONArray();
        JsonObject innerObject;
        for(PackageList packageList:selecteditem)
        {
            innerObject=new JsonObject();
            innerObject.addProperty("pack_id",packageList.getSubs_id());
            innerObject.addProperty("pack_dsc", packageList.getSubs_desc());
            innerObject.addProperty("pack_tax_amt", packageList.getTax_amnt());
            innerObject.addProperty("pack_grand_total", packageList.getSubs_grnd_tot_prc());


            Log.i("INNEROBJECT","hyu"+innerObject);

            packsJSON.put(innerObject);

            Log.i("PACKJSON","nkd==>>"+packsJSON)
        }

INNEROBJECT的输出:

{"pack_id":"39","pack_dsc":"350 Package","pack_tax_amt":"0","pack_grand_total":"419"}

PACKJSON的输出:

 [ "{\"pack_id\":\"39\",\"pack_dsc\":\"350 Package\",\"pack_tax_amt\":\"0\",\"pack_grand_total\":\"419\"}",
  "{\"pack_id\":\"2232\",\"pack_dsc\":\"Bangara(280)\",\"pack_tax_amt\":\"0\",\"pack_grand_total\":\"280\"}"
 ]
 [
  "{"pack_id":"39","pack_dsc":"350 Package","pack_tax_amt":"0","pack_grand_total":"419"}",
  "{"pack_id":"2232","pack_dsc":"Bangara(280)","pack_tax_amt":"0","pack_grand_total":"280"}"
 ]

3 个答案:

答案 0 :(得分:1)

您的JSONArray是org.json软件包的,而JsonObject是com.google.gson软件包的 因此您的innerObject的类型应为org.json.JSONObject

    JSONArray packsJSON  = new JSONArray();
    JSONObject innerObject;
    for(PackageList packageList:selecteditem)
    {
        innerObject=new JSONObject();
        innerObject.put("pack_id",packageList.getSubs_id());
        innerObject.put("pack_dsc", packageList.getSubs_desc());
        innerObject.put("pack_tax_amt", packageList.getTax_amnt());
        innerObject.put("pack_grand_total", packageList.getSubs_grnd_tot_prc());


        Log.i("INNEROBJECT","hyu"+innerObject);

        packsJSON.put(innerObject);

        Log.i("PACKJSON","nkd==>>"+packsJSON)
    }

答案 1 :(得分:0)

请检查下面的答案。

where c.[Channel Status] = '5'
                and c.[Channel Type] = '1'
                and (c.[Channel Category ID] =@category or @category IS NULL)
                and (c.Surface between @sqm*0.85 and @sqm*1.15 or @sqm IS NULL)
                and (c.[Long term price per month] between @longRentPrice*0.85 and @longRentPrice*1.15 or @longRentPrice IS NULL)
                and (c.[Short term Price per night] between @shortRentPrice*0.85 and @shortRentPrice*1.15 or @shortRentPrice IS NULL)
                and (r.[Actual Date in] > @fromDate and r.[Actual Date out] > @toDate)
                and (r.[Actual Date in] < @fromDate and r.[Actual Date out] < @toDate)

输出

enter image description here

答案 2 :(得分:0)

我陷入了类似的问题。尽管下面的解决方案可以即时运行(调试模式->表达式求值->所有反斜杠都被修剪掉,但是在分配给变量outputString时,它仍然具有那些反斜杠)

String outputString = jsonString.replaceAll(“ \\”,“”);  Log.e(“ Clear String:”,outputString);

感谢您的帮助。