This question already has answers here:
Parse Json string in C#
(5 answers)
Closed 5 years ago.
How can i parse the following string to json object in C#:
string unescapedstring = " {"SettingName":"name","SettingValue":"\\log1\\log2\\","Description":"description"}"
to get:
{
"SettingName": "name",
"SettingValue": "\\log1\\log2\\",
"Description": "description"
}
Thanks.
You can use JObject class and its method .parse which accepts string as argument:
JObject jo = JObject.Parse(unescapedstring);
Newtonsoft_Json Library
Related
This question already has answers here:
How to escape braces (curly brackets) in a format string in .NET
(11 answers)
String.Format: Input string was not in a correct format [closed]
(5 answers)
Closed 3 years ago.
I'm getting an exception "Input string was not in a correct format". I don't understand why.
I looked at the documentation, but that didn't give me any ideas.
string body = string.Format(#"{
""credentials"": {
""name"": ""{0}"",
""password"": ""{1}"",
""site"": {
""contentUrl"": ""{2}""
}
}
}",Username, Password, siteName);
Not enough sleep and old code... I was deserializing the response but not serializing the request 🤦♂️
{ and } have special meanings in a format string. If you want the string to contain them literally, you have to escape them by doubling them:
string body = string.Format(#"{{
""credentials"": {{
""name"": ""{0}"",
""password"": ""{1}"",
""site"": {{
""contentUrl"": ""{2}""
}}
}}
}}", Username, Password, siteName);
Alternatively, as #mjwills has suggested, you can create an anonymous object and serialize this to JSON:
var obj = new { name = Username, password = Password, site = new { contentUrl = siteName }};
string body = new JavaScriptSerializer().Serialize(obj);
This question already has an answer here:
Parsing ISO Duration with JSON.Net
(1 answer)
Closed 6 years ago.
I got the following json result from a rest api and I am using Newtonsoft.Json to deserialize it to a c# object.
{
"d": {
"results": [
{
"Aufnr": "4000103",
"Workdate": "/Date(1482796800000)/",
"Beguz": "PT07H30M00S",
}
]
}
}
Aufnr and Workdate are working with String and DateTime, but I got no clue which datatype to use for Beguz
I tried TimeSpan and DateTime and got this error: Error converting value "PT07H30M00S" to type 'System.TimeSpan'. Path '[0].Beguz'
Any ideas?
This is a pure string:
public string Beguz { get; set; }
Of course if you want this PT07H30M00S string to be represented by some complex custom structure you could write a custom JsonConverter to achieve this task. In this converter you will need to provide the logic of how to parse this string back to some custom structure of yours.
This question already has answers here:
Parse Json string in C#
(5 answers)
Closed 6 years ago.
i have json string as following
string json = "{\"ID\":\"hid\",\"Specification\":\"hname\"}";
but i want to read Id and hname as following
string hid = hardwareidTextbox.Text;
string hname = hardwarename_Textbox.Text;
how do i read variable vales in JSON string
Try using
JsonConvert.DeserializeObject(json);
This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 6 years ago.
Folks,
I am trying to access a json value from Azure ML rest service but i get a null value all the time, i tried different options but it did not work. Can u please provide ideas.
Json String
{
"Results": {
"output1": {
"type": "table",
"value": {
"ColumnNames": ["Sentiment",
"Score"],
"ColumnTypes": ["String",
"Double"],
"Values": [["negative",
"0.20"],
**["negative",
"0.03"]**]
}
}
}
}
Trying to fetch the value between **
Tried the below.
using Newtonsoft.Json.Linq;
JObject o = JObject.Parse(sentimentvalue);
string valv = (string)o.SelectToken("Results[0].output1[0].Values[0]");
Your JSON path in the SelectToken seems to be wrong.
Try this:
string valv = (string)o.SelectToken("$.Results.output1.value.Values[0][1]");
This question already has answers here:
Force JSON.NET to include milliseconds when serializing DateTime (even if ms component is zero)
(3 answers)
Closed 7 years ago.
I am trying to parse JSON string into JObject using JObject.Parse().
But its not working ,This is my code:
string json="{\"hashkey\":\"paphAsethE2rexev6c5qAbayu3ebEc\",\"expiration\":\"2016-11-24T12:00:00.000Z\"}";
JObject resourceJson = JObject.Parse(json);
return Ok(resourceJson);
but in the output some value of expiration is missing that is(.000)
Expected Result:
{
"hashkey": "paphAsethE2rexev6c5qAbayu3ebEc",
"expiration": "2016-11-24T12:00:00.000Z"
}
the result which i am getting is:
{
"hashkey": "paphAsethE2rexev6c5qAbayu3ebEc",
"expiration": "2016-11-24T12:00:00Z"
}
please help
You can set the DateTime format in WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
//...
var converter = new Newtonsoft.Json.Converters.IsoDateTimeConverter
{DateTimeFormat="yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"};
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(converter);
// ...
}