Json.NET doesn't work?

FndFnd TWMember

Hi,

I have a rpoblem for Json,

I try to Convert a string to a model for exanple:

public class JsonClass { public string str1 { get; set; } public string str2 { get; set; } public Obj obj1 { get; set; } public List<Array> arr1 { get; set; } } public class Obj { public string str3 { get; set; } public string str4 { get; set; } } public class Array { public string str5 { get; set; } public string str6 { get; set; } }
But it cant Convert in iOS

(I use same string and model in Android,and it works)

anyone can tell me what is my problem?

Posts

  • FndFnd TWMember

    error msg:

    Object reference not set to an instance of an object

  • Could you post your JSON and the code so we can look if there's a problem in there?

  • FndFnd TWMember

    I tried to make that model become a same one but different name,

    Then I saw a strange thing...

    http://imgur.com/TvIHSeJ

  • nodoidnodoid GBMember, Beta ✭✭✭

    By the looks of it your deserialisation object isn't being set up (hence the nulls). Do your deserialization classes match up to what is coming down from the server? Run the query through the json2csharp website and this will generate the classes required for you.

    What puzzles me is the title of this thread - what are you passing into JSON.NET to get this to fail?

  • FndFnd TWMember
    edited August 2014

    In fact,I used it already,

    I am lazy,json2csharp is good for me~~

    This title is because first of my question is Json.Net cant Convert it,

    but it work on xamarin.Android (yes,I copy it from my front project)

    so I am sure the question is not from model or input string,

    that's why I have no idea for this bug...T__T

    my code is very simple...

    mdlogin Json = JsonConvert.DeserializeObject<mdlogin>(wc.DownloadString(wa.getUrl(0)));

  • FndFnd TWMember
    edited August 2014

    this is Exception Message

    "Unable to find a constructor to use for type WSC_iOS.Model.mdlogin. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'DeptName', line 1, position 12."

    Json string:

    {"DeptName":"","DeptID":"","Avatar":"","FullName":"","Outside_Flag":"","status":{"code":"","msg":""},"MenuList":[{"cID":"","cName":"****","cIconURL":"","cSerURL":"","cSort":"","cNotify":"","detailUrl":""},{"cID":"","cName":"","cIconURL":"","cSerURL":"","cSort":"","cNotify":"","detailUrl":"****"},{"cID":"","cName":"***","cIconURL":"","cSerURL":"","cSort":"","cNotify":"","detailUrl":"****"}]}

    model class:

    `using System.Collections.Generic;

    namespace WSC_iOS.Model
    {

    public class mdlogin
    {
        public string DeptName { get; set; }
        public string DeptID { get; set; }
        public string Avatar { get; set; }
        public string FullName { get; set; }
        public string Outside_Flag { get; set; }
        public Status status { get; set; }
        public List<MenuList> MenuList { get; set; }
    }
    
    public class Status
    {
        public string code { get; set; }
        public string msg { get; set; }
    }
    
    public class MenuList
    {
        public string cID { get; set; }
        public string cName { get; set; }
        public string cIconURL { get; set; }
        public string cSerURL { get; set; }
        public string cSort { get; set; }
        public string cNotify { get; set; }
        public string detailUrl { get; set; }
    }
    

    }`
    StackTrace:

    " at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in :0 \n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in :0 \n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in :0 \n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in :0 "

  • I tried the models you provided and the json and was able to deserialize without any problems. I think you should update your JSON.NET library and then do a Rebuild and try again.

  • FndFnd TWMember

    I tried it ,but dont works,

    It was so strange bug...

    I'll try other solution,

    thx for all of ur answers~~~

  • FndFnd TWMember

    Hi all,

    I try to make a new project and move all in that,

    then...It works...=________=

    I dont know why but I solved this bug,

    Thanks all of your answer~~>(")<

Sign In or Register to comment.