Convert Newtonsoft.JSON.Linq.Jarray to a List of Specific Object Type

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



Leave a reply



Submit