Convert Newtonsoft.Json.Linq.JArray to a list of specific object type
Just call array.ToObject<List<SelectableEnumItem>>()
method. It will return what you need.
Documentation: Convert JSON to a Type
Newtonsoft.Json.Linq.JArray to string array C#
If model.Users
is of type Newtonsoft.Json.Linq.JArray
try to call:
string[] Users = model.Users.ToObject<string[]>()
Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List`
JArray jsonResponse = JArray.Parse(goldString);
foreach (var item in jsonResponse)
{
JObject jRaces = (JObject)item["races"];
foreach (var rItem in jRaces)
{
string rItemKey = rItem.Key;
JObject rItemValueJson = (JObject)rItem.Value;
Races rowsResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Races>(rItemValueJson.ToString());
}
}
How to convert JArray to generic List
You can convert a JObject
to a type of your choosing with the .ToObject<T>()
method.
https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToObject__1_1.htm
In this case you want your code to look like this:
listMedia.Add(item.ToObject<MediaInfo>());
Convert JSON result to object type
The main problem here is that you are trying to access result as an object, but it is actually an array containing a single object. So you need to do this to get the object.
JToken ojObject = joResponse["result"][0];
But there is another problem, in that there is no field called number
in your JSON. I think the value you want is actually in a field called display_value
. So then you also need to use this.
string ticketnumber = ((JValue)ojObject.SelectToken("display_value")).Value.ToString();
You can even shorten this to the following;
string ticketnumber = (string)joResponse.SelectToken("result[0].display_value");
How to convert Newtonsoft.Json.Linq.Array to an array of double?
You can try something like this:
var input = $"[[\"17.623665\",\"87.380157\",\"51.530350\",\"121.286850\",\"+0.480\"],[\"89.199364\",\"47.660252\",\"123.106056\",\"81.566940\",\"+0.521\"]]";
var jarray = JsonConvert.DeserializeObject<JArray>(input);
foreach (var arrayItem in jarray)
{
var innerArray = arrayItem.ToObject<double[]>();
foreach (var item in innerArray)
{
Console.WriteLine(item);
}
}
You would have to filter out the last empty item first:
[
"\r\n"
]
"\r\n" does not deserialize to a double..
Related Topics
How to Get the Correct Ip from Http_X_Forwarded_For If It Contains Multiple Ip Addresses
Vb.Net Equivalent to C# Var Keyword
Argumentoutofrangeexception on Initialized List
Changing the Cursor in Wpf Sometimes Works, Sometimes Doesn'T
What Is Myassembly.Xmlserializers.Dll Generated For
How to Get Status Code from Webclient
"An Internal Error Occurred." When Loading Pfx File with X509Certificate2
What Happens If I Return Before the End of Using Statement? Will the Dispose Be Called
Print List of Objects to Console
How to Embed Gecko or Webkit in a Windows Form Just Like a Webview
Complex Models and Partial Views - Model Binding Issue in ASP.NET MVC 3
Inheriting Comments from an Interface in an Implementing Class
Sortedlist<>, Sorteddictionary<> and Dictionary<>
Use Task.Run() in Synchronous Method to Avoid Deadlock Waiting on Async Method
How to Modify Existing Xml File with Xmldocument and Xmlnode in C#