简体   繁体   English

反序列化 json 对象 xamarin android c#

[英]Deserialize json object xamarin android c#

I have little issue with deserialize json object.我对反序列化 json 对象没有什么问题。 My json from http url:我的 json 来自 http url:

Screen of my downloaded JSON我下载的 JSON 的屏幕

I don't know how to deserialize to make dynamically creating buttons.我不知道如何反序列化以动态创建按钮。 I figure out how to create buttons with text, but I don't know how to make them with options that they have.我想出了如何使用文本创建按钮,但我不知道如何使用他们拥有的选项来制作它们。 I try to get these options in Windows Form app for test, but app will crash.我尝试在 Windows 窗体应用程序中获取这些选项进行测试,但应用程序会崩溃。 Thank you for help.谢谢你的帮助。

Using Newtonsoft.NET :使用Newtonsoft.NET

var obj = JsonConvert.DeserializeObject(json);

You can also make a pairing class and use generics:您还可以创建一个配对类并使用泛型:

public JsonClass {
    // Do this for each property you want to map.
    [JsonProperty(PropertyName="id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName="name")]
    public int Name { get; set; }

    [JsonProperty(PropertyName="type")]
    public MessageType Message { get; set; }
}

public class MessageType {
    [JsonProperty(PropertyName="id")]
    public int Id { get; set; }
    // etc...
}

then do:然后做:

JsonClass obj = JsonConvert.DeserializeObject<JsonClass>(json);
MessageType messageType = obj.Message;

Your classes should be something like:你的课程应该是这样的:

public class Type
{
    public int id { get; set; }
    public string name { get; set; }
    public bool closedQuestion { get; set; }
    public bool multiAnswer {get; set;}
    public bool usesImage {get; set; }
}
public class RootObject
{
    public int id { get; set; }
    public string name { get; set; }
    public Type type { get; set; }
    public List<string> options { get; set; }
}

Then you should be able to deserialize your json, using Newtonsoft.Json:然后你应该能够使用 Newtonsoft.Json 反序列化你的 json:

List<RootObject> myData = JsonConvert.DeserializeObject<List<RootObject>>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM