How to deserialize a class inherit from java.lang.object?

BlackShawarnaBlackShawarna ITUniversity ✭✭

Hi all!

Do you know why Json.Deserialize didin't work with class that inherit from java.lang.object?

An easy example

MyClass

public class FoodItem
    {
        public int ID { get;set; }
        public string Name { get;set; }
        public double Price { get;set; }
    }

If i will use this code:

var letto = Newtonsoft.Json.JsonConvert.DeserializeObject <Food[]> (json);

It works!
But, if i change my class into this

 public class FoodItem : Java.Lang.Object
        {
            public int ID { get;set; }
            public string Name { get;set; }
            public double Price { get;set; }
        }

Then the code won't run.

Someone could explain me why this happen? Thank you

Best Answer

Answers

  • BlackShawarnaBlackShawarna ITUniversity ✭✭

    Thank you, unfortunately the program didn't recognize the attributes.
    I've got my Json.net component installed in my project but even if i use using Newtonsoft.Json; the attributes didn't work.

  • PaullnPaulln GBMember ✭✭

    The DataContract and DataMember attributes are referenced from System.Runtime.Serialization if you haven't already referenced it.

  • BlackShawarnaBlackShawarna ITUniversity ✭✭

    Hi @Paulln thanks for reply but it didn't work. I already have that using referenced. These are all of them

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    

    Unfortunately the program doesn't like the attributes. I already delete bin and obj folders, clean the solution...but it just can't recognize them.

  • BlackShawarnaBlackShawarna ITUniversity ✭✭

    ok just found out the problem. it was my assembly reference. Thank you all

  • ibazar1983ibazar1983 Member

    @Paulln said:
    The DataContract and DataMember attributes are referenced from System.Runtime.Serialization if you haven't already referenced it.

    What if you can't change the source code of the class?

Sign In or Register to comment.