none
DataContractJsonSerializer - problems with escaped characters RRS feed

  • Question

  • Hi, I have a problem when using DataContractJsonSerializer. I have this JSON string:

    			"content_id": 20264,
    			"section_id": 10,
    			"section_name": "Music & Gigs",
    			"title": "Charice Embraces Her Musical Liberation via 'Chapter 10'",
    			"synopsis": "International singing sensation Charice is loud and proud to share with the world her newest album under Star Records titled \"Chapter 10.\"","author":{"name":"","slug":"","url":""},"thumb_url":"http: \/\/gallery.clickthecity.com\/albums\/userpics\/10010\/CTC-20264-image2.jpg","post_date":"2013-09-2600: 00: 00","post_date_formatted":"Thu,
    			26Sep201312: 00AM"},{"content_id":20239,"section_id":10,"section_name":"Music&Gigs","title":"TheKillersAnnounceDirectHitsSetForNovember11thRelease","synopsis":"Tenyearsago,
    			onSeptember16th,
    			2003,
    			fouryoungmenfromLasVegasleftAmericaforthefirsttimetoplaytheirfirsteverLondonshow,
    			appearingfourthonthebillatCamden\u2019sDublinCastle.ItwasthefirsttimetheyhadplayedagigoutsideofNorthAmerica.","author":{"name":"","slug":"","url":""},"thumb_url":"http: \/\/gallery.clickthecity.com\/albums\/userpics\/10010\/CTC-20239-image2.jpg","post_date":"2013-09-2500: 00: 00","post_date_formatted":"Wed,
    			25Sep201312: 00AM"}],"favorites":[]},"message":"Ok","status":true}

    I am used online JSON viewers to check whether this JSON is correct and it is. However, when using my code to handle JSON, it fails. I think it is caused by the escape characters like

    \"Chapter 10.\"

    in the synopsis.

    This is my code:

     public static async Task<T> JsonDeserialize<T>(Uri uri)
            {
                using (var http = new HttpClient())
                {
                    string jsonString = await http.GetStringAsync(uri);                
                    jsonString = jsonString.Replace("\\\\", "\\").Replace("\\\"", "\"");
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                    ms.Position = 0;
                    T obj = (T)ser.ReadObject(ms);
                    return obj;
                }
            }
    (this code works when there's no escape characters inside JSON string)

    I need help please. Thank you!

    Monday, October 7, 2013 5:56 AM

All replies

  • What is the source of the JSON string?  It seems like it is coming from a non-C# application, which is responsible for the different escape characters.  A character escape sequence like this one is not being handled by your method:

    \/\/gallery


    If my response was helpful, please vote it as helpful. If I have answered the question being asked, please mark my post as the answer.

    Monday, October 7, 2013 7:15 PM
  • Hi! thanks for your response, however, this is not the problem. I tried changing my code to:

    public static async Task<T> JsonDeserialize<T>(Uri uri)
            {
                using (var http = new HttpClient())
                {
                    string jsonString = await http.GetStringAsync(uri);
                    jsonString = jsonString.Replace("\\\\", "\\");
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                    ms.Position = 0;
                    T obj = (T)ser.ReadObject(ms);
                    return obj;
                }
            }

    removing Replace("\\\"", "\"");

     on the original code. And now, it works. However, I am now unable to convert \" to ". \" is being read as it is. Thus, problem is still not solved.
    Monday, October 7, 2013 7:23 PM