1

I'm having a few problems trying to consume my JSON Data from a web URL and put it into my Class Array.

My class looks something like this;

public class User
{
     String Name;
     String Serial;
     String Email;
}

Where my JSON data looks like

{ "name": "cname", "value": [ "Joe Bloggs"] },
{ "name": "serialnumber", "value": [ "231212312" ] },
{ "name": "gender", "value": [ "male" ] },
{ "name": "email", "value": [ "[email protected]" ] },

I want to pop this into a User Class Array so that it would be somthing like

User myUsers[] = new User[100];

I have the data downloaded using a StreamReader, but I'm lost as to where to start. I've tried out DataContractJsonSerializer and a few others but can't find any basic guides on the web where to start.

I should note that I want to only grab the values listed in the class and not extras such as Gender etc.

If someone could provide a basic sample of both the class and the program implementation to read the data that would be great.

Thanks,

CM888.

3
  • Curious, why are all your values single entry arrays? Are they where the different users are? I.e. {"name":"cname", "value": ["Joe", "Bob"]} would represent 2 different users? Commented Apr 7, 2011 at 0:50
  • Ahh looking at the DB everyone starts with Name first then the Attribute the value Commented Apr 7, 2011 at 1:17
  • I've expanded my question from the comments in the answer. Commented Apr 7, 2011 at 16:50

1 Answer 1

2

I Highly Reccomend looking into this library:

Json.NET

It has many great features, but the best is that it's designed to mimic LINQ to XML. You can use it very similarly.

Using this library you could parse your json into objects and work with them & linq queries to build up your user array.


To expand on my comment above: (Unrelated to question or answer)

What i meant was i was curious why your JSON wasn't strctured like this:

[
  {"cname": "Joe Bloggs", "serialnumber": "231313213", "gender": "male", "email": "[email protected]"},
  {"cname": "Another Dude", "serialnumber": "345345345", "gender": "male", "email": "[email protected]"}
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.