简体   繁体   English

从Web API解析C#中的JSON.Net

[英]Parsing JSON.Net in C# from web API

I am trying to parse a JSON obtained from a web API but I do not know how to do it correctly, I am learning C# and I am using JSON.Net, this is my JSON: 我试图解析从Web API获得的JSON,但我不知道如何正确地做,我正在学习C#而我正在使用JSON.Net,这是我的JSON:

{
    "success": true,
    "result": {
        "id": "20429683581",
        "name": "CINEPLEX S.A",
        "Condition": "HABIDO",
        "PLE": "02\/01\/2013",
        "lawyers": [
            {
                "type": "DNI",
                "numdoc": "07871885",
                "name": "PONCE PINTO ALEJANDRO EDUARDO",
                "date": "22\/08\/2000"
            },
            {
                "type": "DNI",
                "numdoc": "09333203",
                "name": "SORIANO BARRANTES JOSE FERNANDO",
                "date": "22\/09\/2008"
            }
        ],
        "workers": [
            {
                "time": "2017-07",
                "service": "8"
            },
            {
                "time": "2018-06",
                "service": "13"
            }
        ]
    }
}

In the example, the API returned 2 "lawyers" and 2 "workers" but this number can vary, they can be 3 or 4 independently (the other section of the json remains constant). 在这个例子中,API返回了2名“律师”和2名“工人”,但这个数字可以变化,它们可以独立地为3或4(json的另一部分保持不变)。 With PHP I know very well how to solve this but in C# I do not have much idea, this is the code that I'm using to parse it (I've parsed except "lawyers" and "workers"...) 使用PHP我非常清楚如何解决这个问题,但是在C#中我没有太多的想法,这是我用来解析它的代码(除了“律师”和“工人”之外我已经解析了......)

public class Lawyer
{
    public string type { get; set; }
    public string numdoc { get; set; }
    public string name { get; set; }
    public string date { get; set; }
}

public class Worker
{
    public string time { get; set; }
    public string service { get; set; }
}

public class Result
{
    public string id { get; set; }
    public string name { get; set; }
    public string Condition { get; set; }
    public string PLE { get; set; }
    public List<Lawyer> lawyers { get; set; }
    public List<Worker> workers { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public Result result { get; set; }
}

RootObject rootobject;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/api/?get=" + inid);
HttpWebResponse response;

try
{
    response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    {
        var json = reader.ReadToEnd();
        rootobject = JsonConvert.DeserializeObject<RootObject>(json);
    }
    if (rootobject.success == true)
    {
        Console.WriteLine("---------------");
        Console.WriteLine("ID: " + rootobject.result.id);
        Console.WriteLine("NAME: " + rootobject.result.name);
        Console.WriteLine("CONDITION: " + rootobject.result.Condition);
        Console.WriteLine("PLE: " + rootobject.result.PLE);
    }
    else
    {
        Console.WriteLine("---------------");
        Console.WriteLine("NOTHING");
    }
}
catch (Exception)
{
    Console.WriteLine("---------------");
    Console.WriteLine("ERROR");
}

What should I do here? 我该怎么办? Would I have to use a foreach like in PHP? 我是否必须像PHP一样使用foreach? As I said, the amount of "lawyers" or "workers" can be variable. 正如我所说,“律师”或“工人”的数量可以变化。

Pd: To generate the initial user classes json2csharp.com Pd:生成初始用户类json2csharp.com

from what I can tell from your comments, you have deserialized this. 从你的评论我可以看出,你已经反序化了这一点。 You are just having trouble traversing the object model which means the whole json.net thing is a bit of a side track, to traverse, just do something like 你只是在遍历对象模型时遇到了麻烦,这意味着整个json.net的东西都是一个侧面轨道,要遍历,只需做类似的事情

rootobject.result.lawyers.ForEach(lawyer => Console.WriteLine($"{lawyer.name} {lawyer.type}");

or you can do 或者你可以做

foreach(var lawyer in rootobject.result.lawyers) 
{
     Console.WriteLine($"{lawyer.name} {lawyer.type}");
}

or good ol for loops if you wish 如果你愿意,还可以选择好的循环

for(int i = 0; i<rootobject.result.lawyers.Count; i++)
{
   var lawyer = rootobject.result.lawyers[i];
   // print it...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM