0

I am converting DataSet to JSon using this code.

JObject jsonObject = new JObject();
string jsonString = string.Empty;

JsonSerializerSettings jsonSettings = new JsonSerializerSettings();
jsonSettings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
jsonString = JsonConvert.SerializeObject(dsResults, jsonSettings);
jsonObject = JObject.Parse(jsonString);
if (!string.IsNullOrEmpty(identifier))
   jsonObject.AddFirst(new JProperty("identifier", identifier));

if (!string.IsNullOrEmpty(resourceType))
   jsonObject.AddFirst(new JProperty("resourceType", resourceType));

if (!string.IsNullOrEmpty(patientId))
{
   jsonObject.AddFirst(new JProperty("patientId", patientId));
   jsonObject.AddFirst(new JProperty("status", "'{success: true}'")); 
}
jsonString = jsonObject.ToString();

And, it is giving me output (appending Dataset name and all the things within quotes) as;

{
  "TableName": [
    {
       "status": "{success: true}",
       "softwareName": "MY Software",
       "softwareVersion": "0.4.5.9",
       "TimeZone": "(UTC+12:00) City, Country"      
    }
  ]
}

But, I want output like this

{
   status: {success: true},
   softwareName: "My Software ",
   softwareVersion: "0.4.5.9",
   TimeZone: (UTC+12:00) City, Country
}

What is problem in above code or what modifications can I do? I don’t want to achieve my results with jsonString.Replace("","") or indexing approach.

J.SMTBCJ15
  • 471
  • 6
  • 20
  • Why do you use serialization to json string and later write that do not want to use. –  Mar 01 '17 at 07:51
  • Actually, I wan't to serialize this but, I'm having this in quotes and Dataset name @Breakermind – J.SMTBCJ15 Mar 01 '17 at 07:54
  • http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes – Wim Ombelets Mar 01 '17 at 07:57
  • Try create string from variables dont use json serialize string s = "{ \"Name\" : 123 "+variable+"}"; –  Mar 01 '17 at 07:57
  • @WimOmbelets I know this, but as mentioned this is my requirement to pass this to our service provider/consumer – J.SMTBCJ15 Mar 01 '17 at 07:58
  • @Breakermind I don't wan't to build it by myself as there are still more requirements coming from our service provider and almost all of them have this syntax which they'll process further – J.SMTBCJ15 Mar 01 '17 at 08:00
  • Now I'm curious as to what they're actually requiring you to pass because it sure ain't json. – Wim Ombelets Mar 01 '17 at 08:02
  • No wait strike that... It should be emphasized, that what you end up with is, in fact, NOT json. the [RFC](https://tools.ietf.org/html/rfc7159) clearly states that "An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array." If they wish to process it further, let them but it's bad practice to indulge in other people's bad practice. Dare I quote Obi-Wan in this: "Who's the more foolish? The fool or the fool who follows him?" – Wim Ombelets Mar 01 '17 at 08:15
  • 1
    @WimOmbelets ... Thanks for reminding about Obi-Wan quote, but, you know _might is right_ . The man dealing with us there is Technically NON-TECHNICAL – J.SMTBCJ15 Mar 01 '17 at 09:49

1 Answers1

0

You should probably avoid it as it is not compliant with the JSON standard.

That said, if you need to go ahead with it there's a great solution here by Christophe Geers: https://stackoverflow.com/a/7555096/4636912

Using the Json.NET library you can achieve this as follows:

[JsonObject(MemberSerialization.OptIn)] public class ModalOptions {
[JsonProperty]
public object href { get; set; }

[JsonProperty]
public object type { get; set; } }

When serializing the object use the JsonSerializer type instead of the static JsonConvert type.

For example:

var options = new ModalOptions { href = "file.html", type = "full" };
var serializer = new JsonSerializer(); var stringWriter = new
StringWriter(); using (var writer = new JsonTextWriter(stringWriter))
{
   writer.QuoteName = false;
   serializer.Serialize(writer, options);
} 
var json = stringWriter.ToString();

This will produce:

{href:"file.html",type:"full"}

If you set the QuoteName property of the JsonTextWriter instance to false the object names will no longer be quoted.

Community
  • 1
  • 1