【问题标题】:Parsing Multiple Json Rows in windows phone在 Windows Phone 中解析多个 Json 行
【发布时间】:2013-06-19 04:41:38
【问题描述】:

我从 php 生成的 json 响应:

{"name":"abhi","age":"20","id":"1"}
{"name":"abhi","age":"21","id":"4"}

而c#代码是:

public partial class MainPage : PhoneApplicationPage
  {
     // Constructor

    public MainPage()
     {

        InitializeComponent();
     }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Load(textBox1.Text);
    }

    public void Load(string keyword)
    {
        var client = new RestClient("http://localhost/query.php?name="+keyword);
        var request = new RestRequest(Method.GET);
        //request.AddParameter("name", keyword);
        /*request.AddParameter("v", "1.0");
        request.AddParameter("q", keyword);
        request.AddParameter("hl", "id");
        request.AddParameter("rsz", 5);*/
        client.ExecuteAsync<RootObject>(request, (response) =>
        {



               // var resp = response.Data.ToString();
               // var respLines = resp.Split('\n');
                RootObject rootObject=response.Data;
                listBox1.Items.Clear();

                    if (rootObject == null)
                    MessageBox.Show("null");
                    else
                    {
                        listBox1.Items.Add(rootObject.age+" " + rootObject.name);
                    }


        });

    }


}
}
public class RootObject
{
  public string name { get; set; }
  public string age { get; set; }
  public string id { get; set; }
}

我能够获取第一行但不能获取多行。任何人都知道如何以 json 格式获取多行。如何创建多个json对象并填充它们?

【问题讨论】:

    标签: c# json windows-phone


    【解决方案1】:

    这看起来不像是有效的 JSON,请尝试从 PHP 返回一个 JSON 数组:

    {
      "rows": [
        {"name":"abhi","age":"20","id":"1"},
        {"name":"abhi","age":"21","id":"4"}
      ]
    }
    

    这些将是用于反序列化的相应 C# 类:

    public class Row
    {
        public string name { get; set; }
        public string age { get; set; }
        public string id { get; set; }
    }
    
    public class RootObject
    {
        public List<Row> rows { get; set; }
    }
    

    你可以添加你的行:

    foreach (var row in rootOject.rows)
    {
      listBox1.Items.Add(row.age+" " + row.name);
    }
    

    【讨论】:

    • 你能告诉我如何以那种格式返回一个数组吗?我当前的 php 代码是
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2012-02-22
    相关资源
    最近更新 更多