使用Newtonsoft.JSON在C#中的泛型方法中反序列化Object <t>(字符串s)不起作用

时间:2017-01-22 16:44:32

标签: c# json unity3d json.net

  

目前我只是将json字符串返回到调用Test1()的相应文件,并将其反序列化为ResponseClass r = JsonConvert.DeserializeObject(response_json)   发送部分我忘记上课[Serializable]。现在工作正常。

1部分:

public class Movie
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Classification { get; set; }
    public string Studio { get; set; }
    public DateTime? ReleaseDate { get; set; }
    public List<string> Genres{ get; set; }
}


public class ResponseClass
{
    public string SuccessStatus{ get; set; }
    public string next_link { get; set; }
}


private void Test1<T,Q>()
{
    string json = @"{
      'Name': 'Bad Boys',
      'ReleaseDate': '1995-4-7T00:00:00',
      'Genres': [
        'Action',
        'Comedy'
      ]
    }";


   //Here making network call with above json and getting correct response_josn
    Q response_obj = JsonConvert.DeserializeObject<Q>(reponse_json);
    print(response_obj);
}

我在点击按钮时调用Test1(),如下所示:

Test1<Movie, ResponseClass>();

对于上面的示例,我将打印日志作为ClassName + Movie(T FullName)。 我想将字符串反序列化为该类。如何实现?

enter image description here

第2部分:如果我的课程为:

[Serializable]
public class Movie
{
    public string Name;
    public string Description;
    public string Classification;
    public string Studio;
    public DateTime ReleaseDate;
    public SubClass subClass;
    public List<SubClass> lsubclass;
}
[Serializable] //This was the mistake. 
public class SubClass
{
    public string a;
    public string b;
    public List<string> ReleaseCountries;
}

private Movie createValidMovieJson()
{
    Movie m = new Movie();

    SubClass sc = new SubClass();
    sc.a = "aaa";
    sc.b = "bbb";
    sc.ReleaseCountries = new List<string>();
    sc.ReleaseCountries.Add("Japan");
    sc.ReleaseCountries.Add("India");
    List<SubClass> lsC = new List<SubClass>();
    lsC.Add(sc);
    lsC.Add(sc);

    m.Name = "Bad Boys";
    m.Studio = "Pixa";
    m.subClass = sc;
    m.lsubclass = lsC;
    Debug.Log(JsonUtility.ToJson(m));  // value n log = {"Name":"Bad Boys","Description":"","Classification":"","Studio":"Pixa"}
    return m;
}
在使用ToJson()后,

JsonUtility返回空值代替子类,如上面的函数所示。

2 个答案:

答案 0 :(得分:1)

根据您添加的屏幕截图,我认为您希望能够将反序列化类型视为电影。这是实现这一目标的方法:

var movie = JsonConvert.DeserializeObject<Movie>(json);

目前,您的反序列化对象被视为类型T - 由于您的方法没有泛型类型约束,因此可以是任何内容。

答案 1 :(得分:0)

就像我在评论部分中所说,JsonUtility应该这样做。

  

我刚刚替换了T m = JsonConvert.DeserializeObject(json);与T   m = JsonUtility.FromJson(json);它给出了一个错误   ArgumentException:JSON解析错误:缺少对象成员的名称。

您的json对JsonUtility无效。我相信您使用的是'而不是"。这就是您收到此错误的原因。

使用以下函数生成有效的json:

void createValidMovieJson()
{
    Movie m = new Movie();
    m.Name = "Bad Boys";
    m.ReleaseCountries = new List<string>();
    m.ReleaseCountries.Add("Japan");

    m.Studio = "Pixa";
    Debug.Log(JsonUtility.ToJson(m));
}

你会得到:

 {"Name":"Bad Boys","Description":"","Classification":"","Studio":"Pixa","ReleaseCountries":["Japan"]}

ecaped进行测试时,您将获得:

{\"Name\":\"Bad Boys\",\"Description\":\"\",\"Classification\":\"\",\"Studio\":\"Pixa\",\"ReleaseCountries\":[\"Japan\"]}

要使JsonUtility生效,您必须将[Serializable]添加到该类,并从其类变量中删除{ get; set; }

如果您的目标是将任何json转换为任何数据类型,则必须返回泛型类型,然后使用Convert.ChangeType将其转换为该类型。

看起来应该是这样的:

// Use this for initialization
void Start()
{
    string json = "{\"Name\":\"Bad Boys\",\"Description\":\"\",\"Classification\":\"\",\"Studio\":\"Pixa\",\"ReleaseCountries\":[\"Japan\"]}";

    Movie movie = Load<Movie>(json);
    print(movie.Name);
}

[Serializable]
public class Movie
{
    public string Name;
    public string Description;
    public string Classification;
    public string Studio;
    public DateTime? ReleaseDate;
    public List<string> ReleaseCountries;
}

private T Load<T>(string json)
{
    object resultValue = JsonUtility.FromJson<T>(json);
    return (T)Convert.ChangeType(resultValue, typeof(T));
}