PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

[FIXED] How can i convert an array in Json string to c# object?

 July 26, 2022     c#, json     No comments   

Issue

I have a json return result like the following:

Json =

{ "Id":"12345", "FirstName":"Bob", "LastName":"Builder", "Links":[] }

Links can have a list of objects of type LinkService, i want to cast even if the array is empty , so in c# i get an empty array.

I do the following

      var token = JObject.Parse(Json);
      var Id = token.Value<string>("Id");
      var fname = token.Value<string>("FirstName");
      var lbname = token.Value<bool>("LastName");
      var links = JsonSerializer.Deserialize<List<LinkService>>(token.Value<Array>("Links"));

Issue is that it says cant convert System.Array to Newtonsoft.Json.JsonReader


Solution

You can convert the jToken to an object using the ToObject method:

var links = token["Links"].TObject<List<LinkService>>();


Answered By - Amir Popovich
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing