繁体   English   中英

如何存储和解析 json 响应

[英]how to store and parse an json response

我目前正在尝试解析此链接的响应https://api.ote-godaddy.com/v1/domains如果您想了解有关 GoDaddy api go 到https 的更多信息://developer.godaddy.com/doc

现在在我的程序中我应该得到这种类型的响应:

[
  {
    "createdAt": "2015-06-15T13:10:43.000Z",
    "domain": "000.biz",
    "domainId": 1002111,
    "expirationProtected": false,
    "expires": "2016-06-14T23:59:59.000Z",
    "exposeWhois": false,
    "holdRegistrar": false,
    "locked": true,
    "nameServers": null,
    "privacy": false,
    "renewAuto": true,
    "renewable": false,
    "status": "TRANSFERRED_OUT",
    "transferAwayEligibleAt": "2016-07-29T23:59:59.000Z",
    "transferProtected": false
  }
,
  {
    ""createdAt"": ""2015-06-15T13:10:43.000Z"",
    ""domain"": ""000.biz"",
    ""domainId"": 1002111,
    ""expirationProtected"": false,
    ""expires"": ""2016-06-14T23:59:59.000Z"",
    ""exposeWhois"": false,
    ""holdRegistrar"": false,
    ""locked"": true,
    ""nameServers"": null,
    ""privacy"": false,
    ""renewAuto"": true,
    ""renewable"": false,
    ""status"": ""TRANSFERRED_OUT"",
    ""transferAwayEligibleAt"": ""2016-07-29T23:59:59.000Z"",
    ""transferProtected"": false
  }
]

我需要解析它以获取“域”属性并将其写入。这是我想出的:

const string WEBSERVICE_URL = "https://api.ote-godaddy.com/v1/domains?statuses=&includes=";
try
{
    var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
    if (webRequest != null)
    {
        webRequest.Method = "GET";
        webRequest.Timeout = 12000;
        webRequest.ContentType = "application/json";
        webRequest.Headers.Add("Authorization", "sso-key " + api_key + ":" + api_secret);

        using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
            {
                var jsonResponse = sr.ReadToEnd();
                if (jsonResponse == "[]")
                {
                    Console.WriteLine("No domains found");
                    bad = +1;

                }
                else
                {
                    var jo = JObject.Parse(jsonResponse);
                    var domain = jo["domain"];
                    hit = +1;
                    Console.WriteLine(api_key + ":" + api_secret + "=" + domain);
                }
            }
        }
    }
}

(不要担心像 api_key 这样的其他变量。一切正常)不幸的是我得到这个错误:

Newtonsoft.Json.JsonReaderException:从 JsonReader 读取 JObject 时出错。 当前 JsonReader 项目不是 object:StartArray。 路径 '',第 1 行,position 1。

我想获得每个域。 希望有人能帮助我,ty。

创建一个 class 来代表您的 json。您可以通过复制 json > Edit > Paste Special > Paste JSON as Classes 来完成此操作。

public class Domain
{
    public DateTime CreatedAt { get; set; }
    public string Domain { get; set; }
    public int DomainId { get; set; }
    public bool ExpirationProtected { get; set; }
    public DateTime Expires { get; set; }
    public bool ExposeWhois { get; set; }
    public bool HoldRegistrar { get; set; }
    public bool Locked { get; set; }
    public object NameServers { get; set; }
    public bool Privacy { get; set; }
    public bool RenewAuto { get; set; }
    public bool Renewable { get; set; }
    public string Status { get; set; }
    public DateTime TransferAwayEligibleAt { get; set; }
    public bool TransferProtected { get; set; }
}

然后反序列化

  var myDomain = JsonConvert.DeserializeObject<Domain>(jsonResponse);

...
    
  Console.WriteLine(api_key + ":" + api_secret + "=" + myDomain.Domain);

对于一系列域

  var myDomains = JsonConvert.DeserializeObject<IEnumerable<Domain>>(jsonResponse);

...
    
  Console.WriteLine(api_key + ":" + api_secret + "=" + myDomains.FirstOrDefault().Domain);

数据是一个数组,因此您需要使用JArray而不是JObject

JArray jo = JArray.Parse(jsonResponse);
Console.WriteLine(jo[0]["domain"]);

https://do.netfiddle.net/tKAix8

暂无
暂无

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

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