用多个值读取json

时间:2017-05-15 09:30:42

标签: c# json interface

我的方法中有这段代码片段。它应该编写列表test.E_id的所有部分,但它什么都不写。我不知道我做错了什么。

string jsonText = File.ReadAllText(jsonFilePath);
Execution test = JsonConvert.DeserializeObject<Execution>(jsonText);
    foreach (string eID in test.E_id)
    {
        Console.WriteLine(eID);
    }

这是我的执行类,除了编写字符串工作正常。

public class Execution
{
    public string Usr_id { get; private set; }
    public string Patient_id { get; private set; }
    public List<string> E_id { get; private set; }
    public List<string> E_title { get; private set; }
    public List<string> E_description { get; private set; }
    public List<string> E_date { get; private set; }
    public List<string> E_delete { get; private set; }

    public Execution(string usr_id, string patient_id, List<string> e_id, List<string> e_title, List<string> e_description,
    List<string> e_date, List<string> e_delete)
    {
        Usr_id = usr_id;
        Patient_id = patient_id;
        E_id = e_id;
        E_title = e_title;
        E_description = e_description;
        E_date = e_date;
        E_delete = e_delete;
    }
}

这是我想读的json文件:

{
    "usr_id":"573",
    "patient_id":"170510024",
    "executions":[
        {
            "id":"SF70H",
            "title":"Verbandswechsel",
            "description":"Verband des rechten Armes wechseln",
            "date":"2017-07-28T12:00:00.000Z",
            "delete":false
        },
        {
            "id":"SF18H",
            "title":"Physiotherapie",
            "description":"Beweglichkeit des Knies wiederherstellen",
            "date":"2017-07-28T14:00:00.000Z",
            "delete":false
        }
    ]

也许有人知道我做错了什么,可以帮我找到错误。     }

3 个答案:

答案 0 :(得分:2)

您的问题是您的JSON和代码彼此不匹配。你有单个字段的列表,你有错的名字等等。

使用此C#映射到您的JSON(从json2csharp.com生成):

public class Execution
{
    public string id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public DateTime date { get; set; }
    public bool delete { get; set; }
}

public class Test
{
    public string usr_id { get; set; }
    public string patient_id { get; set; }
    public List<Execution> executions { get; set; }
}

然后使用此C#代码:

Test test = JsonConvert.DeserializeObject<Test>(jsonText);

答案 1 :(得分:1)

您的代码结构与您的JSON结构不匹配。由于您已经了解了JSON文件的结构,因此Visual Studio中有一个方便的工具可以帮助您从中生成代码。

选择文件中的JSON结构 - &gt; Ctrl + C - &gt; Visual Studio - &gt;编辑 - &gt;选择性粘贴 - &gt;将JSON粘贴为类。

这给出了以下结果:

public class Rootobject
{
    public string usr_id { get; set; }
    public string patient_id { get; set; }
    public Execution[] executions { get; set; }
}

public class Execution
{
    public string id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public DateTime date { get; set; }
    public bool delete { get; set; }
}

现在您可以使用JsonProperty属性对其进行装饰,并将名称设置为C#标准:

public class Rootobject
{
    [JsonProperty("usr_id")]
    public string UserId { get; set; }
    [JsonProperty("patient_id")]
    public string PatientId { get; set; }
    [JsonProperty("executions ")]
    public Execution[] Executions { get; set; }
}

public class Execution
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("title")]
    public string Title { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("datee")]
    public DateTime Date { get; set; }
    [JsonProperty("delete")]
    public bool Delete { get; set; }
}

答案 2 :(得分:0)

此代码运行良好:

public class ExecutionOrder
{
    public string Usr_id { get; set; }
    public string Patient_id { get; set; }
    public List<Execution> Executions { get; set; }

    public ExecutionOrder(string usr_id, string patient_id, List<Execution> executions)
    {
        Usr_id = usr_id;
        Patient_id = patient_id;
        Executions = executions;
    }

}

public class Execution
{
    public string E_id { get; set; }
    public string E_title { get; set; }
    public string E_description { get; set; }
    public string E_date { get; set; }
    public bool E_delete { get; set; }

    public Execution(string e_id, string e_title, string e_description, string e_date, bool e_delete)
    {
        E_id = e_id;
        E_title = e_title;
        E_description = e_description;
        E_date = e_date;
        E_delete = e_delete;
    }

}

但是现在我有另一个问题,我想从Execution对象中读取字符串和bool并在CommandWindow中打印它。我可以毫无问题地打印Usr_id和Patient_id,并且还会显示来自对象的bool。但它不打印字符串。它是从对象中读取字符串的正确方法吗?

ExecutionOrder test = JsonConvert.DeserializeObject<ExecutionOrder>(jsonText);
test.Executions.ForEach(delegate(Execution exec)
{
    Console.WriteLine(test.Usr_id);
    Console.WriteLine(test.Patient_id);
    Console.WriteLine(exec.E_id);
    Console.WriteLine(exec.E_title);
    Console.WriteLine(exec.E_description);
    Console.WriteLine(exec.E_date);
    Console.WriteLine(exec.E_delete);
    Console.WriteLine();
});