将数组从 Web 服务请求 C# 转换为字符串
convert an array to a string from a web service request C#
我有一个 php 网络服务,我尝试使用它并将数据解析到设计器中的文本框(我在 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"
}
]
}
我在将数组列表响应转换为字符串 rootObject 时遇到问题,请问您有什么想法吗
感谢帮助
您可以为 sponsorises
添加另一个 class:
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;
问题是 JsonConvert.Deserialize() 需要一个字符串,而不是数组列表。
这可以通过不将您的 Web 响应投射到 ArrayList 来完成
var response = await http.GetStringAsync(uriString);
var rootObject = JsonConvert.DeserializeObject<RootObject>(response);
将其添加到每个 RootObject 属性的开头:
[JsonProperty("fieldName")]
我有一个 php 网络服务,我尝试使用它并将数据解析到设计器中的文本框(我在 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"
}
]
}
我在将数组列表响应转换为字符串 rootObject 时遇到问题,请问您有什么想法吗 感谢帮助
您可以为 sponsorises
添加另一个 class:
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;
问题是 JsonConvert.Deserialize() 需要一个字符串,而不是数组列表。
这可以通过不将您的 Web 响应投射到 ArrayList 来完成
var response = await http.GetStringAsync(uriString);
var rootObject = JsonConvert.DeserializeObject<RootObject>(response);
将其添加到每个 RootObject 属性的开头:
[JsonProperty("fieldName")]