0

I'm trying to serialize a list of objects into json, but in the json all of the data members are surrounded by "\". For example, my object is:

public class ResObject
{
    public double productPrice { get; set; }
    public string productName { get; set; }
    public double distance { get; set; }
    public string fullAddress { get; set; }
    public string fullStoreName { get; set; }
    public string promotion { get; set; }
}

and the resulted json is:

"[{\"productPrice\":4,\"productName\":\"Milk\",\"distance\":0,\"fullAddress\":\"UK\",\"fullStoreName\":\"LiaStore\",\"promotion\":null},...]"

I used this codes:

string json = JsonConvert.SerializeObject(Products, Formatting.None);

OR

var json = new JavaScriptSerializer().Serialize(Products);

OR

    //Create a stream to serialize the object to.  
    MemoryStream ms = new MemoryStream();

    // Serializer the User object to the stream.  
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<ResObject>));
    ser.WriteObject(ms, Products);
    byte[] json = ms.ToArray();
    ms.Close();

    return Encoding.UTF8.GetString(json, 0, json.Length);

but it did not helped :(

(Products is List<ResObject> Products)

Thank you!

EDIT: I fixed it, the problem was that I serialized list and returned it and since it is WebAPI it was the problem. The fix was to return the list and delete the serialize.

Aviv
  • 5
  • 2
  • 2
    Probably a duplicate of [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182). – dbc Sep 27 '18 at 21:08
  • The backslash, must be when you view the value in debug mode in Watch. Is that correct? – Praveen Paulose Sep 27 '18 at 21:08

1 Answers1

-1

Let me guess ... you looked at the string in the debugger. Just print it out to the console or write it to a file and you‘ll see, that those „\“ are gone.

It‘s just an escape charakter shown in the debugger.

This way, you can put a ‘ “ ‘ into s string.

Andreas
  • 828
  • 4
  • 15