ScuffedDouleur
New member
- Joined
- Nov 13, 2022
- Messages
- 4
- Programming Experience
- Beginner
I'm trying to create a c# framework and a necessity I need is the ability to write with json in C# and if I can how can I go about doing so.
var json = @"
{
'myArray' : [
{ 'key' : 'value' },
{ 'key' : 'value' )
]
}
";
So your original post is invalid?A quick follow-up. C# 11 now has raw string literals that simplifies embedding JSON into C# code.
Strings - C#
Learn about strings in C# programming. See information on declaring and initializing strings, the immutability of string objects, and string escape sequences.learn.microsoft.com
var j = JsonConvert.SerializeObject(new {
myInt = 1,
myString = "a",
myObject = new {
anotherInt = 1
},
myArray = new[]{ 1,2,3 },
myObjArray = new[]{ new { a = 1}, new { a = 2 } }
});
var j = JsonConvert.SerializeObject(new {
myObject = my.SomeMethodThatReturnsAnObject()
});
var myObj = my.SomeMethodThatReturnsAnObject();
var j = JsonConvert.SerializeObject(new {
myObject = new {
a = myObj.A,
b = myObj.Bee
}
});
var myObj = my.SomeMethodThatReturnsAnObject();
var j = @"{
myObject: {
a: " + myObj.A + @"
b:" + myObj.Bee + @"
}
}";
And for good reason but you could always alternatively engage in some replacement of the ' to " to make it valid again, or embed the string in something that understands strings (text file, embedded resource type thinger) to put straight json in there..C# doesn't have an easy way to embed JSON within the C# code
C# doesn't have an easy way to embed JSON within the C# code.
And for good reason
<Query>
<Where>
<Geq>
<FieldRef Name="Field Name"/>
<Value Type="DateTime">
<Today/>
</Value>
</Geq>
</Where>
<OrderBy>
<FieldRef Name="Field Name"/>
</OrderBy>
</Query>