1

I'm using a .NET WCF service to return a string that contains a JSON coming from my DB in MongoDB. It works well but when I do a GET I get a Json file with \" instead of simple "

Eg. I get \"id\" instead of "id"

Here's my code:

string IDeviceService.GetDeviceList()
{
        IMongoCollection<BsonDocument> collection = DatabaseManager.DeviceCollection();
        string deviceList = string.Empty;
        var devices = collection.Find(new BsonDocument()).ToList();

        foreach (var device in devices)
        {
            string json = device.ToString();
            deviceList = (deviceList + json);
        }

        return deviceList;
}

I've tried to do a .Replace("\\", "") that should do the trick but it doesn't do anything.

Any idea?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex
  • 21
  • 2
  • `\"` means the quote is being escaped. So if its inside the content you _should_ leave it escaped so it wont terminate and cause malformed JSON. If the key is being escaped then you should look at whether it was inserted into the DB correctly in the first place – maccettura Jun 27 '18 at 19:50
  • Regarding your `.Replace("\\", "")` not working: Strings are immutable, so you have to assign the result of that method to a variable. But, yeah, removing the escaping backslash might not be a good idea, depending on the context – Sentry Jun 27 '18 at 19:53
  • It would help if you posted a sample of the json you are getting back. I know you showed `\"id\"` but it really helps to know the context of what/where that is in the structure – maccettura Jun 27 '18 at 19:55

2 Answers2

0

The Json I'm getting back:

"{ \"_id\" : ObjectId(\"5b31ee511e7c9ad0bf63b17a\"), \"id\" : \"01\", \"name\" : \"device1\", \"deviceType\" : \"presenceSensor\" }{ \"_id\" : ObjectId(\"5b31ee511e7c9ad0bf63b179\"), \"id\" : \"14\", \"name\" : \"Device14\", \"deviceType\" : \"humiditySensor\" }{ \"_id\" : ObjectId(\"5b31ee511e7c9ad0bf63b17b\"), \"id\" : \"02\", \"name\" : \"device2\", \"deviceType\" : \"humiditySensor\" }{ \"_id\" : ObjectId(\"5b31ee511e7c9ad0bf63b17e\"), \"id\" : \"03\", \"name\" : \"device3.1\", \"deviceType\" : \"temperatureSensor\" }{ \"_id\" : ObjectId(\"5b31f7371e7c9ad0bf63b361\"), \"id\" : \"04\", \"name\" : \"device4\", \"deviceType\" : \"lightSensor\" }"
Alex
  • 21
  • 2
0

I think I had a similar issue here: Remove escape characters and quoatation marks from JSON

I also tried to fix it with .Replace() but I don't think you can use that to remove escape characters.

Basically I fixed it by changing the return type to Stream instead of string and then returning the json string like this:

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(deviceList));

So instead of return deviceList; use the above lines and change string IDeviceService.GetDeviceList() to stream IDeviceService.GetDeviceList()

Or something like that atleast. Your WCF is different from mine I'm sur so there might be more stuff.

Der Kejser
  • 63
  • 10
  • I've tried that, I either get something like this `{ "__identity": null, "_buffer": [ 123, 32, 34, 95, 105, 100, 34, 32, 58, 32,` or a `Unexpected 'O'` – Alex Jun 28 '18 at 11:47