3

I'm having trouble reading a JSON list of numbers into a c# int[] array.

I've tried several suggestions from SO, but none have worked. How would I go about this using JSON.net?

Extract from JSON file:

    {
        "course": "Norsk",
        "grades": [6, 3, 5, 6, 2, 8]
    }

What I've tried in c#:

// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);

// Parsing the information to a format json.net can work with
JObject data = JObject.Parse(json);

JToken jToken = data.GetValue("grades");
jGrades = jToken.Values<int>().ToArray();

and:

// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);

// Parsing the information to a format json.net can work with
JObject data = JObject.Parse(json);

for (int o = 0; o < 6; o++) {
    var grades = from p in data["Info"[i]] select (int)p["grades"[o]];
    jGrades.Add(Convert.ToInt32(grades));
}

As you can see from the c# extracts, I've tried with both arrays and lists, but I can't get it to work.

With the first example (with an array) I get a System.NullRefrenceException, while with the List example, I get several errors, such as Unable to cast object of type 'whereselectlistiterator'2 [Newtonsoft.JSON] to type 'system.iconvertible'

Any help of tips are appreciated.

2 Answers 2

7

JObject.Parse(json) is your root object

JObject.Parse(json)["grades"] is the list/array

All you have to do is : converting the items to appropriate type

var list = JObject.Parse(json)["grades"].Select(x => (int)x).ToArray();

You can also declare a class

public class RootObject
{
    public string course { get; set; }
    public List<int> grades { get; set; }
}

and deserialize whole object as

var myobj = JsonConvert.DeserializeObject<RootObject>(json);
var grade = myobj.grades[0];
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, I tried your first solution, but end up getting a System-ArgumentNullException error. And if this worked how would I go about accessing this info seeing as it is a var and not an array? Can I index it and add it to a list? Such as: var list... grades.Add(list[i]);?
@Hagland a) above codes work with the json in your question, b) stackoverflow.com/questions/4307467/what-does-var-mean-in-c
thanks read up on var, but no, the code doesn't work: imgur.com/a/wuXHw
@Hagland So you think this difference is not important!!. OK good luck. Whenever you decide to ask a good question we will be here
@Hagland do you think jobj["someprop"] will find the values anywhere in json. Json is a hierarchical representation and the without knowing the whole structure a correct answer can not be given... If you don't want to post the real json, then open the docs and read them. This is all you can get from us with the info you have provided.
|
4

I would typically define a class with the relevant properties and simply convert the object.

public class CourseReport
{
     public string Course { get; set; }
     public ICollection<int> Grades { get; set; }
}

// Reads the JSON file into a single string
string json = File.ReadAllText(jfile);
Console.WriteLine(json);

// Parsing the information to a format json.net can work with
var courseReport = JsonConvert.DeserializeObject<CourseReport>(json);

foreach (var grade in courseReport.Grades)
{
     Console.WriteLine(grade);
}

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.