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?

sansactions
  • 215
  • 5
  • 17

3 Answers3

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.

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70
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.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    Note - answer written using the original version of the question, which you have now edited to change your `Person` type. – dbc Feb 08 '17 at 14:28
  • this worked, for which a big thanks, i do have a question. Here i wanted to get a List 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. – sansactions Feb 09 '17 at 11:54
  • Well it's a list so you can do `person = persons[0]`. Or use [`SingleOrDefault()`](https://msdn.microsoft.com/en-us/library/bb342451(v=vs.110).aspx) instead of `ToList()`. – dbc Feb 09 '17 at 13:54
  • i did the list.first() which i find ugly, the .singleOrDefault() works, thanks! – sansactions Feb 09 '17 at 14:17
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.

sensei
  • 7,044
  • 10
  • 57
  • 125