首页 > 解决方案 > 如何解析 Json 响应并存储一些属性

问题描述

我目前正在尝试解析来自此链接的响应:https ://api.ote-godaddy.com/v1/domains 。

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

[
  {
    "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
  }
]

我需要解析它以获取每个 domain属性并编写它。这就是我想出的:

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
                {
                    JArray jo = JArray.Parse(jsonResponse);
                    hit = +1;
                    string hit_txt = "";
                    hit_txt = api_key + ":" + api_secret + "=" + jo[]["domain"];
                                        

                    Console.WriteLine(hit_txt);
                    using (StreamWriter writetext = new StreamWriter("hit.txt"))
                    {
                       writetext.WriteLine(hit_txt);
                    }
                }
            }
        }
    }
}

(不要担心其他变量,如api_key,一切正常)

不幸的是,我收到了这个错误:

Newtonsoft.Json.JsonReaderException:从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象:StartArray。路径 '',> 第 1 行,位置 1。

标签: c#json

解决方案


在给定的场景中,可以使用以下方式选择来自 Json 的特定值JArray

JArray jo = JArray.Parse(jsonResponse);

//Get all "domain" elements from the Json
var domainNames = string.Join(", ", jo 
                     .Select(jt => jt["domain"])
                     .ToList());

//Then use this comma separated "domainNames" in the string
string hit_txt = "";
hit_txt = api_key + ":" + api_secret + "=" + domainNames;

如果需要获取该元素的集合,请不使用string.Joinas:

var domainNames = jo 
                  .Select(jt => jt["domain"])
                  .ToList();

该系列可以在任何地方进一步使用。


推荐阅读