0

json:

[{
    "PersonsTable":[
    {"id":293,"firstname":"jos","lastname":"don"},
    {"id":1861,"firstname":"jef","lastname":"dan"},    
    {"id":1896,"firstname":"janine","lastname":"din"}]
}]

code:

List<Person> persons = new List<Person>();
dynamic dynObj = JsonConvert.DeserializeObject(response);
foreach (var data in dynObj.PersonsTable)
{
     Person p = new Person(data.id, data.firstname, data.lastname);
     persons.Add(p);
}

Object:

 public class Person
{
    public Person ()
    {

    }
    public Person (string id, string firstname, string lastname)
    {
        this.id= id;
        this.firstname = firstname;
        this.lastname = lastname;
    }
    public string id{ get; set; }
    public string firstname{ get; set; }
    public string lastname{ get; set; }
}

I want to put the data under "PersonsTable" into the person list. I have tried to achieve this with serialize and dynamic variables but i always get a weird error "Missing compiler required member, 'microsoft.CSharp.RUntimeBinder.CSharpArgumentINfo.Create'"..

The NuGet package itself i can't install because my project runs in .Net 3.5 (for some reason).

Can someone help me with my problem? Are there other ways to get a list of persons in result?

3
  • 1
    maybe it because in json there are no PK_R_PERSON, FIRSTNAME, LASTNAME , but there is only id, firstname, lastname Commented Feb 8, 2017 at 14:14
  • completely my fault, i changed the names so it would be nicer for people to look at here, in the code its all the same (in caps) Commented Feb 8, 2017 at 14:21
  • @dbc just me being bad at copy pasting Commented Feb 8, 2017 at 14:23

3 Answers 3

5

Your problem is not related to json parsing I think.

As you are using the keyword "dynamic", you must have in your project a reference to Microsoft.CSharp.dll.

See here for example : C# dynamic compilation and "Microsoft.CSharp.dll" error

update : I see you have updated your question since I've posted my answer. You now say you are running in .Net 3.5. To be clear, dynamic is NOT AVAILABLE in .Net 3.5. See Use of Dynamic Keyword in .Net 3.5 for example.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a few problems:

  1. The names of the properties of your c# classes do not match the property names in the JSON. (Note - fixed in the edited version of your question.)

  2. Your root JSON container is an array containing a single object, not the object itself. You need to account for the extra level of nesting when parsing.

  3. You say you are running on .Net 3.5 which does not support dynamic.

Rather than using dynamic, you can explicitly parse to a JToken then manually map to your Person type using LINQ to JSON with SelectTokens():

var root = JToken.Parse(response);
var persons = root
    // Use the JSONPath wildcard operator to select all entries in the "PersonsTable"
    .SelectTokens("[*].PersonsTable[*]")
    // And map the individual entry to a Person type.
    .Select(data => new Person((string)data["id"], (string)data["firstname"], (string)data["lastname"]))
    .ToList();

Even if you were able to use dynamic, by doing so you lose compile-time error checking. Using statically defined methods may lead to fewer unexpected run-time errors.

Sample fiddle.

4 Comments

Note - answer written using the original version of the question, which you have now edited to change your Person type.
this worked, for which a big thanks, i do have a question. Here i wanted to get a List<Person> as a result, what if i only want Person as a result (lets say i have 1 entry in the JSON), i tested it and just getting rid of (.ToList();) and or adding (Person) before root didn't do it.
Well it's a list so you can do person = persons[0]. Or use SingleOrDefault() instead of ToList().
i did the list.first() which i find ugly, the .singleOrDefault() works, thanks!
0

Create new viewModel with field List PersonsTable {get; set;}, then accept it on endpoint, it will automatically map the model, altought you might have to add [JsonProperty(PropertyName = "id")], to your Person class members for proper mapping.

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.