0

我有一个 php Web 服务,我试图在设计器中使用并将数据解析到我的文本框(我在 Windows 10 应用商店应用程序上工作),这是我的代码:

public sealed partial class MainPage : Page
    {
        public string uriString = "my URL";
        public MainPage()
        {
            this.InitializeComponent();
            Getdata();
        }

        private async void Getdata()
        {
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            System.Collections.ArrayList response = new System.Collections.ArrayList(new string[] { await http.GetStringAsync(uriString) });
            var rootObject = JsonConvert.DeserializeObject<Sponsorises.RootObject>(response); //the error is here
            sponsorname.Text = rootObject.nom; //the name of my textBox
        }
        public class Sponsorises
     {

    internal class RootObject
    {
        public string nom { get; set; }
        public string tel { get; set; }
        public string photo { get; set; }
        public string sponsorise_adrs { get; set; }

    }
}

这是我的 json 代码:

 {
success: 1,
message: "sponsorise found!",
total: 1,
sponsorises: [
{
nom: "my third local",
tel: "88888888",
photo: "http://192.168.1.1/1446241709_ab_cart2_bleu2.png",
sponsorise_adrs: "the adress"
}
]
}

我在将 arraylist 响应转换为字符串 rootObject 时遇到问题,您有什么想法吗,谢谢您的帮助

4

3 回答 3

1

您可以添加另一个类sponsorises

internal class RootObject
{
    public string success { get; set; }
    public string message { get; set; }
    public string total { get; set; }
    public List<Sponsorise> sponsorises { get; set; }
}

class Sponsorise
{
    public string nom { get; set; }

    public string tel { get; set; }

    public string photo { get; set; }

    public string sponsorise_adrs { get; set; }
}

像这样反序列化:

var rootObject = JsonConvert.DeserializeObject<RootObject>(response); 
sponsorname.Text = rootObject.sponsorises[0].nom; 
于 2015-12-04T16:20:33.660 回答
1

问题是 JsonConvert.Deserialize() 需要一个字符串,而不是数组列表。

这可以通过不将您的网络响应投射到 ArrayList 来完成

var response = await http.GetStringAsync(uriString);
var rootObject = JsonConvert.DeserializeObject<RootObject>(response);
于 2015-12-04T16:26:34.270 回答
1

将其添加到每个 RootObject 属性的开头:

[JsonProperty("fieldName")]
于 2015-12-04T16:30:05.047 回答