3

When I am trying to deserialize my JSON with JSON.Net it gives me invalid values for JSON arrays.

I have simple JSON:

[
{
 "PhaseName": "Start",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
},
{
 "PhaseName": "In Progress",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
},
{
 "PhaseName": "Finish",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
}

]

And a correspondent class in my code:

public class Advices
{
 public string PhaseName { get; set; }
 public List<string> AdvicesList { get; set; }
}

And a veriable that represents an array of Advices objects:

public class MyAdvices
{
 private Advices[] MyAdvicesArrays;
....

So when I am trying to deserialize my JSON like that:

MyAdvicesArrays= JsonConvert.DeserializeObject<Advices[]>(sMyJSON));

JSON.Net populates "Phase Name" property of each object in my MyAdvicesArrays array with correct value but Advices array of each object is invalid: "Could not evaluate expression" if you check them in runtime. I don't get it. What I've done wrong?

asked Nov 2, 2012 at 4:17

3 Answers 3

1

Your json is not valid according to the entities.

In your type change AdvicesList to Advices.

public List<string> AdvicesList { get; set; }

Change it to

public List<string> Advices { get; set; }
answered Nov 2, 2012 at 4:24

1 Comment

"Advices" in my example supposed to be an object with string property and array property, how could it be List<string>?
0

No, you're JSON is incorrect as is you Class.

First change your JSON to this:

{
 phases:[
 {
 "PhaseName": "Start",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
 },
 {
 "PhaseName": "In Progress",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
 },
 {
 "PhaseName": "Finish",
 "Advices": ["Lorem ipsum dolor",
 "Lorem ipsum dolor",
 "Lorem ipsum dolor"]
 }
 ]
}

Notice before you weren't declaring you JSON object correctly. Also you need a property for the array.

Now you'll need two classes:

public class AnythinYouWant{
 public List<Phase> Phases{get;set;}
}
public class Phase{
 public string PhaseName{get;set;}
 public List<string> Advices{get;set;}
}

Notice how the first class is for the top level JSON object. The name can be whatever you want. But any property needs to match the name.

answered Nov 2, 2012 at 4:34

2 Comments

Thanks, sounds logical. So does it mean that I cannot have unnamed array of objects in JSON like in my example? [ { "example": 1, "example2": "TestName" }, { "example3": 1, "example4": "TestName" } ]
no you can't, because your json object must always begin with {}, you can't declare it with []. After the initial {} you have to have a property to assign your first array.
0

All I did to get this working was to change the definition of the Advices class:

public class Advice
{
 public string PhaseName { get; set; }
 public List<string> Advices { get; set; }
}

I changed the class name to just Advice and AdvicesList property to Advices.

answered Nov 2, 2012 at 4:38

1 Comment

I don't understand, what have you changed except for class name and property name? The class definition is still the same.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.