Deserialize Array C#

Hi Experts,

Hopefully a simple one.

How does one deserialize the following JSON into a List<string> or a HashSet<int> or ... (fill in object of choice here) in JSON.NET

"myArray":[[7],[3],[6],[5]]

Right Now my class has this property set to:

[JsonProperty]
public JArray myArray {get; set;}

but would prefer it typed/converted to something in the first sentence.


Thanks,

Chud
chudmarekAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Have you looked at the example below? Does that help?
http://www.newtonsoft.com/json/help/html/SerializingCollections.htm

string json = @"{""key1"":""value1"",""key2"":""value2""}";

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

Console.WriteLine(values.Count);


Console.WriteLine(values["key1"]);
Karrtik IyerSoftware ArchitectCommented:
Here is the sample to convert it to a List<string>
http://www.newtonsoft.com/json/help/html/DeserializeCollection.htm
  string json = @"['Starcraft','Halo','Legend of Zelda']";
  List<string> videogames = JsonConvert.DeserializeObject<List<string>>(json);
  Console.WriteLine(string.Join(", ", videogames.ToArray()));
rotem156Commented:
That looks like a serialized List<int>, try this:

string json = "{"myArray":"[7,3,6,5]"}";
List<int> myList = JsonConvert.DeserializeObject<List<int>>(json);

It looks like that "myArray" is a memeber of a struct/class.

You'll need Json.NET for this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

chudmarekAuthor Commented:
Sorry Guys,

It was an Array of Arrays

This worked for me (knowing that each number was unique)

[JsonProperty]
        public HashSet<HashSet<int>> myArray { get; set; }
chudmarekAuthor Commented:
Partial
chudmarekAuthor Commented:
Mr. Wolfe,

Gotcha. Thanks for the heads up on the Grading Link.

According to the guidelines this is an A grade answer then. I have marked them all as assisted solutions, and my follow up as "Best Solution" in case anyone is ever in the same boat as me. Hope all is ok now.

Regards,

Chud
chudmarekAuthor Commented:
Although Answers were all great and lead me to my final, Rotem's suggestion that "myarray" was a memeber of a struct/class lead me to the lightbulb moment that it was an array of arrays, and as such was graded as best solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.