I want to serialize a message to be sent through AWS SNS module.
public async Task<string> jsonConvert(string message)
{
datamessage datamessage = new datamessage { message = message };
gcmMessage lGcm = new gcmMessage { data = datamessage };
MessageDto messageDto = new MessageDto { GCM = JsonConvert.SerializeObject(lGcm) };
var msg = JsonConvert.SerializeObject(messageDto);
return msg;
}
I'm using this code for the same but the return value is
{"GCM":"{\"data\":{\"message\":\"TestMsg\"}}"}
But I want it as
{"default": "TestMsg", "GCM": "{ \"data\": { \"message\": \"TestMsg\" } }"}
Any help would be appreciated.
The structure for the Json you want tot create should look like this:
Classes:
public class Data
{
[JsonProperty("message")]
public string Message { get; set; }
}
public class GCM
{
[JsonProperty("data")]
public Data Data { get; set; }
}
public class RootObject
{
[JsonProperty("default")]
public string Default { get; set; }
[JsonProperty("GCM")]
public GCM GCM { get; set; }
}
Create the message:
RootObject rootObject = new RootObject
{
Default = "TestMsg",
GCM = new GCM { Data = new Data { Message = "TestMsg" } }
};
Serialize:
var serialized = JsonConvert.SerializeObject(rootObject);
Result
"{\"default\":\"TestMsg\",\"GCM\":{\"data\":{\"message\":\"TestMsg\"}}}"
In your method it would look like this:
public async Task<string> jsonConvert(string message)
{
RootObject rootObject = new RootObject
{
Default = message,
GCM = new GCM { Data = new Data { Message = message } }
};
var msg = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(rootObject));
return msg;
}
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.