简体   繁体   中英

remove \r\n with newline in json string

I created a JSON string in C# using this code.

string jsonFormatted = JValue.Parse(JSONresult).ToString(Formatting.Indented);

when I paste the json Formatted string in notepad++, I get lot of \r\n and "\". I want to replace all \r\n with newline. when I tried to replace \r\n with empty space, all \r\n goes away and I can format the string using JSON Viewer -Format JSON plugin, but all \r\n are replaced by LF. Below is the screen shot:

在此处输入图像描述

I want the \r\n to be replaced by new line CRLF. My JSON file is huge so it is difficult to change all \r\n by hand.

在此处输入图像描述

Below is the sample of my partial JSON string:

"{\r\n  \"header\": {\r\n    \"tenantId\": \"23213\",\r\n    \"requestType\": \"PreciseIdOnly\",\r\n    \"clientReferenceId\": \"3243214\",\r\n    \"expRequestId\": \"\",\r\n    \"txnId\": \"\",\r\n    \"messageTime\": \"2020-06-05T19:35:45Z\",\r\n 

Can I do this in either C# or notepad++ or any other editor.

replacing \r\n with empty space or string.empty is not working because the newline character does not come up in notepad++ if I replace the string with \r\n. I want a new line too along with \r\n gone.

Below is my entire JSON file

{
    "header": {
        "tenantId": "23213",
        "requestType": "PreciseIdOnly",
        "clientReferenceId": "3243214",
        "expRequestId": "",
        "txnId": "",
        "messageTime": "2020-06-05T19:35:45Z",
        "options": {}
    },
    "payload": {
        "control": [
            {
                "option": "SUBSCRIBER_PREAMBLE",
                "value": "23213"
            },
            {
                "option": "SUBSCRIBER_OPERATOR_INITIAL",
                "value": "qq"
            },
            {
                "option": "SUBSCRIBER_SUB_CODE",
                "value": "1231"
            },
            {
                "option": "PID_USERNAME",
                "value": "abc"
            },
            {
                "option": "PID_PASSWORD",
                "value": "aaa"
            },
            {
                "option": "PRODUCT_OPTION",
                "value": "24"
            }
        ],
        "contacts": [
            {
                "id": "APPLICANT_CONTACT_ID_1",
                "person": {
                    "typeOfPerson": "",
                    "personIdentifier": "",
                    "personDetails": {
                        "dateOfBirth": "2020-06-05",
                        "yearOfBirth": "",
                        "age": "",
                        "gender": "",
                        "noOfDependents": "",
                        "occupancyStatus": "",
                        "mothersMaidenName": "",
                        "spouseName": ""
                    },
                    "names": [
                        {
                            "id": "",
                            "firstName": "test1",
                            "middleNames": "test2",
                            "surName": "test3",
                            "nameSuffix": ""
                        }
                    ]
                },
                "addresses": [
                    {
                        "id": "Main_Contact_Address_0",
                        "addressType": "CURRENT",
                        "poBoxNumber": "",
                        "street": "42123 test drive",
                        "street2": "",
                        "postTown": "a",
                        "postal": "33232",
                        "stateProvinceCode": "qa"
                    }
                ],
                "telephones": [
                    {
                        "id": "Main_Phone_0",
                        "number": ""
                    }
                ],
                "emails": [
                    {
                        "id": "MAIN_EMAIL_0",
                        "type": "",
                        "email": ""
                    }
                ],
                "identityDocuments": [
                    {
                        "documentNumber": "12321343",
                        "hashedDocumentNumber": "",
                        "documentType": "SSN"
                    }
                ]
            }
        ],
        "application": {
            "productDetails": "",
            "applicants": [
                {
                    "contactId": "APPLICANT_CONTACT_ID_1",
                    "applicantType": "APPLICANT"
                }
            ]
        }
    }
}

Any Help will be highly appreciated

Try Disabling the 'Show All Characters' option in notepad++ to see how your json is getting formatted.

在此处输入图像描述

Same thing happened to me too. What you can do is replace all \r\n with space in notepad++ something like this:

在此处输入图像描述

and then replace all \n with \r\n. Make sure Extended \n\r\ti selected in replace window and you will get all CRLF in your notepad. Let me know if you see any issues

Here is how you can get rid of \r\n by using Regex in C#

string responseString = "your json data which includes \r\n";

// remove \r\n from the string
string data = Regex.Replace(responseString, @"\t|\n|\r", "");
// make \" to "
string refinedString = data.Replace("\\\"", "\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM