【问题标题】:How to parse a Json response and store some attributes如何解析 Json 响应并存储一些属性
【发布时间】:2020-08-18 18:59:16
【问题描述】:

我目前正在尝试解析来自此链接的响应: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
  }
]

我需要解析它以获取 EVERY 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。

【问题讨论】:

  • 正如错误所说,您正在尝试将数组解析为对象。试试JArray.Parse() 之类的东西,而不是JObject.Parse()
  • 是的,但它只在 json 响应中写入第一个域
  • 嗯,你的 JSON 是无效的,因为双双引号太多了。
  • 我知道,我只是错过了提示......
  • 听起来我的评论修复了您在此处询问的错误。如果您还有其他问题,请使用更新的代码发布新问题。

标签: c# json


【解决方案1】:

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

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.Join 作为:

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

该集合可以在任何地方进一步使用。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多