从在Java中形成为数组的字符串中删除斜杠

时间:2019-03-29 10:29:00

标签: java android

我有一个JSON响应。在该JSON块中,有一个包含斜杠(\)的字符串,我尝试了一些解决方案来删除字符串中不需要的数据,但没有成功。

这是我要处理的字符串

"[{\"date\": \"2019-05-14\",\"quantity\": \"55\"},{\"date\": \"2019-05-15\",\"quantity\": \"58\"},{\"date\": \"2019-05-16\",\"quantity\": \"50\"}]"

整个json是:

{
    "Address": "Durga Nagar",
    "Area": "West aerodrome",
    "BookingDate": "1970-01-01",
    "BookingDetails": "[{\"date\": \"2019-05-14\",\"quantity\": \"55\"},{\"date\": \"2019-05-15\",\"quantity\": \"58\"},{\"date\": \"2019-05-16\",\"quantity\": \"50\"}]",
    "noOfBoxes": null,
    "createdAT": "2019-03-29 07:48:07",
    "ModifiedAT": "2019-03-29 07:48:07"
},

我只想删除斜杠。

2 个答案:

答案 0 :(得分:0)

`String ss =“ [{\” date \“:\” 2019-05-14 \“,\” quantity \“:\” 55 \“},{\” date \“:\” 2019- 05-15 \“,\”数量\“:\” 58 \“},{\”日期\“:\” 2019-05-16 \“,\”数量\“:\” 50 \“}]” ;

String replace = ss.replace('\','');`

答案 1 :(得分:0)

    import java.io.FileReader; 
    import org.json.simple.JSONObject; 
    import org.json.simple.parser.*; 

        public class Example 
        { 
            public static void main(String[] args) throws Exception 
            { 
                // parsing file "JSONExample.json" 
                Object obj = new JSONParser().parse(new FileReader("/Users/dell/Documents/JSONExample.json"));          
                // typecasting obj to JSONObject 
                JSONObject jo = (JSONObject) obj; 

                // getting json filds
                String firstName = (String) jo.get("Address"); 
                String lastName = (String) jo.get("Area"); 
                String BookingDate = (String) jo.get("BookingDate"); 

                String BookingDetails = (String) jo.get("BookingDetails"); 
                String noOfBoxes = (String) jo.get("noOfBoxes"); 
                String createdAT = (String) jo.get("createdAT"); 
                String ModifiedAT = (String) jo.get("ModifiedAT"); 

                System.out.println(firstName); 
                System.out.println(lastName); 
                System.out.println(BookingDate); 
                System.out.println(BookingDetails); 
                System.out.println(noOfBoxes); 
                System.out.println(createdAT); 
                System.out.println(ModifiedAT); 

            } 
        } 

result of this file is 
Durga Nagar
West aerodrome
1970-01-01
[{"date": "2019-05-14","quantity": "55"},{"date": "2019-05-15","quantity": "58"},{"date": "2019-05-16","quantity": "50"}]
null
2019-03-29 07:48:07
2019-03-29 07:48:07