简体   繁体   中英

how to remove first and last double quotes in a json string in c#?

I want to remove first and last double quotes in my json string.

"[{\"CircleID\":1,\"CircleName\":\"Andhra Pradesh\"},{\"CircleID\":4,\"CircleName\":\"Assam\"},{\"CircleID\":5,\"CircleName\":\"Bihar\"},{\"CircleID\":6,\"CircleName\":\"Chennai\"},{\"CircleID\":7,\"CircleName\":\"Delhi\"},{\"CircleID\":8,\"CircleName\":\"Gujarat\"},{\"CircleID\":9,\"CircleName\":\"Himachal Pradesh\"},{\"CircleID\":10,\"CircleName\":\"Haryana\"},{\"CircleID\":17,\"CircleName\":\"Mumbai\"},{\"CircleID\":26,\"CircleName\":\"Jharkhand\"},{\"CircleID\":27,\"CircleName\":\"Chhattisgarh\"}]"

in above json string I want to remove first and last double quotes.how to remove.Iam tried but no use. Iam using string.replce method,string.trim() method but quotes are not removed.please help me.

You can use String.Trim do not forget to assign the result back to string variable. string in c# are immutable and we need to assign the changed value back to the variable.

jsonStr = jsonStr.Trim( '"' );

If you have multiple characters you can use overloaded String.Trim(Char[])

jsonStr = jsonStr.Trim( new Char[] { '"' } );

Edit in OP there is only on double quote in beginning and end of string but if we have multiple double quotes in beginning and end and we just want to remove only first and last then we can use string.Substring

jsonStr = jsonStr.Substring(1, jsonStr.Length-2);

this is help for me.

        responseContent = responseContent.Substring(1, responseContent.Length - 1);
        responseContent = responseContent.Substring(0, responseContent.Length - 1);

I'm assuming " and \\" are actually in the string, and not an artifact of embedding that string in source code or displaying it in the debugger.

In that case, you don't want to just remove the initial and trailing " , but you need to treat it as a json string and decode it. This will also replace the encoded \\" by plain " inside the string.

If you're using Newtonsoft Json:

decodedString = JsonConvert.DeserializeObject<string>(jsonString);

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