Matthaousse
Matthaousse

Reputation: 33

Deserialize a JSON Array in c# with Newtonsoft

I just try to deserialize an Json array but it doesn't work. I can read the following message in the output box :

" Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: '. Path '[0]', line 2, position 37."

My code :

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Newtonsoft;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JsonTest
{
    [Activity(Label = "JsonTest", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate
            {
                string json = @"[{
                                   id': '1',
                                   'nom': 'zooz',
                                   'prenom': 'Jack'                                 
                                 }, {
                                   'id': '2',
                                   'nom': 'toto',
                                   'prenom': 'Blaireau'                                 
                                 }]";

                var a = JsonConvert.DeserializeObject<List<Person>>(json);
                //JArray b = JArray.Parse(json);


               Console.WriteLine(a);
                // [email protected]};
            };
        }

        public class Account
        {
            public string id { get; set; }
            public string nom { get; set; }
            public string prenom { get; set; }
        }

        public class Person
        {
            public Account person;
        }


    }
}

Thank for your help.

Upvotes: 0

Views: 1175

Answers (2)

rmc00
rmc00

Reputation: 887

Your JSON is missing a single quote, and it also corresponds to a List<Account> rather than List<Person>. This JSON should deserialize successfully into List

[{ 'person':{
   'id': '1',
   'nom': 'zooz',
   'prenom': 'Jack'                                 
 }}, { 'person': {
   'id': '2',
   'nom': 'toto',
   'prenom': 'Blaireau'                                 
 }}]

Upvotes: 0

Mostafiz
Mostafiz

Reputation: 7352

A single quote missing in id

string json = @"[{
                   'id': '1',
                   'nom': 'zooz',
                   'prenom': 'Jack'                                 
                  }, 
                  {
                    'id': '2',
                    'nom': 'toto',
                    'prenom': 'Blaireau'                                 
                  }]";

also the model Person need to be hold List of Account

 public class Person
  {
      public List<Account> person;
  }

Upvotes: 2

Related Questions