Home > OS >  Deserialize JSON in xamarin
Deserialize JSON in xamarin

Time:01-26

How can i deserialize a local json file to an object? I'm trying to deserialize a local json file in xamarin but it just doesn't work i read many guides and watched some tutorials but none of them helped and most of them give the same code i'm using right now

My code:

         public test()
        {
            InitializeComponent();
            List<Rootobject> ob = new List<Rootobject>();
            var assembly = IntrospectionExtensions.GetTypeInfo(typeof(test)).Assembly;
            Stream stream = assembly.GetManifestResourceStream($"TunisiaPrayer.states.json");
            string text = "";
            using (var reader = new StreamReader(stream))
            {
                text = reader.ReadToEnd();
            }

            ob = JsonConvert.DeserializeObject<List<Rootobject>>(text);

            //printing the json file to make sure it read it fully
            //i get no error here
            DisplayJson.Text = text;
            
            //try catch to test if it was deserialized or not
            try
            {
                //printing a property of ob in a label element to see if it works
                DisplayData.Text = ob[2].Property1.data.gouvernorat.intituleAn;
            }
            catch (Exception ex)
            {
                DisplayData.Text = ex.Message;
            }
        }

RootObject Class:


namespace TunisiaPrayer.Models
{
    public class Rootobject
    {
        public Class1 Property1 { get; set; }
    }

    public class Class1
    {
        public Data data { get; set; }
    }

    public class Data
    {
        public Gouvernorat gouvernorat { get; set; }
        public Delegation[] delegation { get; set; }
    }

    public class Gouvernorat
    {
        public int id { get; set; }
        public string intituleAr { get; set; }
        public string intituleAn { get; set; }
    }

    public class Delegation
    {
        public int id { get; set; }
        public string intituleAr { get; set; }
        public string intituleAn { get; set; }
    }

}

sample of states.json:

[{
        "data": {
            "gouvernorat": {
                "id": 358,
                "intituleAr": "اريانة",
                "intituleAn": "Ariana"
            },
            "delegation": [{
                    "id": 631,
                    "intituleAr": "اريانة",
                    "intituleAn": "Ariana"
                },
                {
                    "id": 534,
                    "intituleAr": "التظامن",
                    "intituleAn": "Attadhamon"
                },
                {
                    "id": 532,
                    "intituleAr": "سكرة",
                    "intituleAn": "Soukra"
                }
            ]
        }
    },
    {
        "data": {
            "gouvernorat": {
                "id": 362,
                "intituleAr": "القصرين",
                "intituleAn": "Kasserine"
            },
            "delegation": [{
                    "id": 579,
                    "intituleAr": "العيون",
                    "intituleAn": "El Ayoun"
                },
                {
                    "id": 576,
                    "intituleAr": "سبيبة",
                    "intituleAn": "Sbiba"
                },
                {
                    "id": 575,
                    "intituleAr": "سبيطلة",
                    "intituleAn": "Sbitla"
                },
                {
                    "id": 573,
                    "intituleAr": "تالة",
                    "intituleAn": "Tala"
                }
            ]
        }
    }]

error: Object reference not set to an instance of an object

note that when i print ob.Count it gives me the correct length of the list but i can't access any data in it

CodePudding user response:

you have to use Root[] , not just root, try this

    var obj =JsonConvert.DeserializeObject< List<Root>>(text);

test

var displayData = obj[1].data.gouvernorat.intituleAn; // = Kasserine

classes

public class Root
{
    public Data data { get; set; }
}
public class Data
{
    public Gouvernorat gouvernorat { get; set; }
    public List<Delegation> delegation { get; set; }
}

public class Gouvernorat
{
    public int id { get; set; }
    public string intituleAr { get; set; }
    public string intituleAn { get; set; }
}

public class Delegation
{
    public int id { get; set; }
    public string intituleAr { get; set; }
    public string intituleAn { get; set; }
}
  •  Tags:  
  • Related