0

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.

2
  • 1
    A single quote (') seems to be missing in 'id':'1' Commented Aug 23, 2016 at 15:07
  • Thanks you. It was a stupid error. Commented Aug 23, 2016 at 15:41

2 Answers 2

2

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;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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'                                 
 }}]

3 Comments

for that case need another model class which hold list of person
The account class has the id, nom, and prenom properties like the JSON. The person class has a single property named person, which is an instance of that account class. So to deserialize as a List<Person>, you have to add that person property which has the JSON structure of an account. Other option is to deserialize as a List<Account>. But there is no need for another model class as far as I can see...
thats true, but take a look at your json in answer

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.