3

I have the following code :

string messageString1 = JsonConvert.SerializeObject(thisComputer);
var data = new { deviceid = "info1", devicetype = "info2", data = messageString1};

My goal is to add messageString1 into data .

thisComputer is a class and i know this part of the code works , because i got it working before , i'm just not being capable to get messageString1 into data.

I am not being able to insert data into data.I have tried several different ways but i still haven't figure it out.

EDIT: the problem is that i am trying to send the JSON to azure IoThub and in fact the contents are getting trough and into IoT Hub but all the double quotes characters are now '\"' and that constitutes the problem .

EXAMPLE: If the data inside is :

{"data":"dataInfo"}

in Iot Hub i see :

{\"data\":\datainfo\"}

Thanks in advance.

Lopes
  • 45
  • 6
  • 2
    What exactly is your problem? Your code example clearly adds messageString1 into data. Your problem description as it is now does not really makes sense. **Edit your question** and clarify your problem description. Especially add an [mcve], **including** an example of input data, the corresponding result you get from that example input data, and the expected result you desire (based on the given example input data). –  May 14 '19 at 16:36
  • 1
    Welcome to Stack Overflow. Please could you provide a [mcve] which shows what you've tried, what's going wrong at the moment, and what you *want* to happen? (That looks like it should have the string inside the anonymous type, but we don't know what you're hoping should happen, or how JArray fits into this.) – Jon Skeet May 14 '19 at 16:37
  • @JonSkeet I'm sorry for the lack of info . I have edited the post . If i'm lacking in any way please let me know . – Lopes May 14 '19 at 16:55
  • It sounds like the problem is that you're applying JSON encoding, and then however you're sending the data to Cloud IoT is doing it again. Have you tried just `var data = new { deviceid = "info1", devicetype = "info2", data = thisComputer };`? – Jon Skeet May 14 '19 at 16:58
  • I see, you serialize your thisComputer to a Json string, then add that json string to the data object, with the whole data object then serialized as json once more (which then would cause any quotation marks `"` within the string to be escaped as `\"`). I suggest to add `thisComputer` directly to `data` (not serialized). Then, when the data object is being json-deserialized, the information from _thisComputer_ will also be properly serialized without additional escaping of quotation marks. (edit: well, i basically suggest the same as Jon Skeet in his comment before me...) –  May 14 '19 at 17:04
  • @JonSkeet no i haven't..until now, and it worked, i was not understanding why that was happening , thank you! – Lopes May 14 '19 at 17:08
  • @elgonzo thank you very much for explanation , i got it to work thanks to you two.Thank you. – Lopes May 14 '19 at 17:21

1 Answers1

2

Currently, you're JSON-encoding an object as a string, and then when you're sending the anonymous type instance to Cloud IoT, that's applying JSON encoding again.

It looks like you don't want the value of data to be a string - you want it to be the data from thisComputer. So just avoid that first level of encoding:

var data = new { deviceid = "info1", devicetype = "info2", data = thisComputer };
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194