【问题标题】:Trying to get a resultset into C# from a php page - not working试图从 php 页面将结果集放入 C# - 不工作
【发布时间】:2018-07-30 22:17:47
【问题描述】:

我在 C# 中有一个从 PHP 页面下载 JSON 对象的函数,只要它只包含一条记录,它就可以正常工作。我正在将 JSON 读入字典。

using (WebClient webClient = new System.Net.WebClient())
{
    WebClient n = new WebClient();
    var json = n.DownloadString("http://127.0.0.1/testjson.php");

    Dictionary<string, string> htmlAttributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

    int ID = Int32.Parse(htmlAttributes["id"]);
    string Phone = htmlAttributes["phone"];
    string Message = htmlAttributes["message"];
    string UID = htmlAttributes["uid"];

    Console.WriteLine(ID);
    Console.WriteLine(Phone);
    Console.WriteLine(Message);
    Console.WriteLine(UID);
}

只要我使用如下 JSON 对象,它就可以正常工作:

{"id":26333,"phone":"+46734231233","message":"Testmeddelande","uid":"A774059B-69CE-40D7-809B-C0A63728F5B9"}

但是当我想解析一个以上的记录时,我得到了错误 - 我得到的错误是:

Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符:[.路径“数据”,第 1 行,位置 9。'

JSON 对象看起来像这样,我已经在线验证了它:

{"data":[{"id":26333,"phone":"+46734231233","message":"Testmeddelande","uid":"A774059B-69CE-40D7-809B-C0A63728F5B9"},{"id":26333,"phone":"+46734231233","message":"Wow, det fungerar","uid":"CA528FCA-CD5D-4220-A646-B556FC060550"}]}

我是新手,所以解决方案可能很明显?

最好的问候,乔金姆

【问题讨论】:

    标签: c# php json dictionary json.net


    【解决方案1】:

    您可以创建一个具体的类,而不是将 json 对象转换为字典对象,如下所示,

    public class DataObject // name it properly
    {
        public int id { get; set; }
        public string phone { get; set; }
        public string message { get; set; }
        public string uid { get; set; }
    }
    
    public class DataList
    {
        public List<DataObject> data { get; set; }
    }
    

    在反序列化的同时,

    DataList objDataList = Newtonsoft.Json.JsonConvert.DeserializeObject<DataList>(json);
    

    在迭代或检索数据时,

    foreach(DataObject objData in objDataList.data)
    {
        Console.WriteLine(objData.ID);
        Console.WriteLine(objData.Phone);
        Console.WriteLine(objData.Message);
        Console.WriteLine(objData.UID);
    }
    

    希望对你有帮助。

    【讨论】:

    • 差不多了,foreach 循环中出现了一些错误。抱怨 objData - 所以我把它改成了另一个变量名。然后它抱怨声明中的数据。
    • 我在这里重新更新了所需的更改。你能测试一下吗?
    • 现在我得到:DataList.data.get 返回 null。
    • 刚刚注意到为什么单条记录和多条记录的json数据不同?您如何在该 PHP 页面中生成 json 数据。如果您有一条记录本身,您应该将它推送到一个数组中,就像您对多条记录所做的那样,然后从那里返回它。你能用多条记录试试吗?
    • 我尝试了单条记录和多条记录,同样的错误。
    【解决方案2】:

    更好的解决方案是复制您想要反序列化的示例 JSON 并利用 Visual Studio 的 paste special 功能:

    您将获得以下生成的代码:

    public class Rootobject
    {
        public Datum[] data { get; set; }
    }
    
    public class Datum
    {
        public int id { get; set; }
        public string phone { get; set; }
        public string message { get; set; }
        public string uid { get; set; }
    }
    

    一旦生成简单的使用以下反序列化和进一步使用:

    string json = "{\"data\":[{\"id\":26333,\"phone\":\"+46734231233\",\"message\":\"Testmeddelande\",\"uid\":\"A774059B-69CE-40D7-809B-C0A63728F5B9\"},{\"id\":26333,\"phone\":\"+46734231233\",\"message\":\"Wow, det fungerar\",\"uid\":\"CA528FCA-CD5D-4220-A646-B556FC060550\"}]}";
    Rootobject htmlAttributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(json);
    
    int ID = htmlAttributes.data[0].id;
    string Phone = htmlAttributes.data[0].phone;
    string Message = htmlAttributes.data[0].message;
    string UID = htmlAttributes.data[0].uid;
    

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多