【问题标题】:Parse Json Response Data解析 Json 响应数据
【发布时间】:2020-09-06 06:23:51
【问题描述】:

我正在调用 Solr Apache 搜索 url,它会转换为 Json 数据格式。但是,当我解析 Json 时,我收到空数据。我的 Json 格式如下:

responseHeader:
   status: 0
   QTime:  1
   params:
      q:  "mykeyword"
 response:
   numFound:  67
   start:     0
   docs:
      0:
          tstamp:  "xxxxx.xxxx.xxxx"
          digest:  "xxxxxxxxxxxxxxx"
          boost:   0.010186654
          id:      "https://myserer/faq.html"
          title:   "xxxx"
          url:     "xxxxxx"
          _version_:"xxxxxx"
          content:  "xxxxxxxxxx"
      1:
         tstamp:  "xxxx"
         .....

所以我创建了dataModel来映射json数据格式:

public class ResponseModel
{
    public ResponseHeader responseheader { get; set; }
    public Response_ responsedata { get; set; }
}
 public class Response_
{
    public int numFound { get; set; }
    public int start { get; set; }
    public Doc doc { get; set; }
}
public class Params
{
    public string  q { get; set; }
}
public class Page
{
    public string tstamp { get; set; }
    public string digest { get; set; }
    public string boost { get; set; }
    public string id { get; set; }
    public string title { get; set; }
    public string url { get; set; }
    public string _version_ { get; set; }
    public string content { get; set; }
}
public class Doc
{
    public List<Page> pages { get; set; }
}

我检索 json 搜索结果的代码:

string baseURL = "http://myserver:8983/solr/nutch/select?q=" + keyword;
string responseBody = string.Empty;
keyword = Request.Form["txtSearchTerm"];
if (!string.IsNullOrEmpty(keyword))
{
    responseBody = getJSONString(baseURL);
}
var myData = JsonConvert.DeserializeObject<ResponseModel>(responseBody);

var Response = myData.responsedata.doc;  //The Response is null here
// ...

private static string getJSONString(string apiURL)
{
    // it returns json string 
}

问题出在哪里?顺便说一句,json 数据中有很多 \n 换行符。这是问题所在以及如何处理它?谢谢

在下方添加 json 数据样本:

{
  "responseHeader": {
    "status": 0,
    "QTime": 0,
    "params": {
      "q": "Intranet"
    }
  },
  "response": {
    "numFound": 19,
    "start": 0,
    "docs": [
      {
        "tstamp": "2020-05-20T01:23:56.427Z",
        "digest": "d615d21052125d3023a6ea5a244a6be0",
        "boost": 0.043801095,
        "id": "https://myserver/services/index.html",
        "title": "Office of Services",
        "url": "https://myserver/services/index.html",
        "content": "Office of Services\nWelcome to the xxxx Website\nAccessibility Navigation:\nSkip to the header\nSkip to the main content\nSkip to the footer\nIt appears that you are viewing this site with an outdated browser.\nUpdate your browser for the best viewing experience by downloading the latest version below:\nInternet Explorer\nGoogle Chrome\nFirefox\nSafari\nMenu\nSearch\nSearch\n  ...\nTop\n",
        "_version_": 1667170768636608512
      },
      {
        "tstamp": "2020-05-20T01:23:56.426Z",
        "digest": "16cc4c01acd01d15ddbc59b7d43b435f",
        "boost": 0.045213405,
        "id": "https://myserver/media/index.html",
        "title": "Library Technical",
        "url": "https://myserver/media/index.html",
        "content": "Library Technical Services Website\nAccessibility Navigation:\nSkip to the header\nSkip to the main content\nSkip to the footer\nIt appears that you are viewing this site with an outdated browser.\nUpdate your browser for the best viewing experience by downloading the latest version below:\nInternet Explorer\nGoogle Chrome\nFirefox\nSafari\nMenu\nSearch\nSearch\n ...  INTRANET\Top\n",
        "_version_": 1667170768619831298
      }
    ]
  }
}

【问题讨论】:

  • 发布您收到的完整 json 响应。
  • 在上面添加json数据

标签: c# asp.net json


【解决方案1】:

问题是您的json 中有一个对象,即response,而在您的c# 类中,您创建了名为responseData 的属性,该属性不会映射,因为它们的名称不同,要么您必须将JsonProperty 名称设置为response,或者您应该将您的属性名称完全更改为response。此外,还有一个 Web 应用程序可以正确解析您的 json 并为您生成相关的 C# 类。以下是我为您共享的响应生成的代码:

public partial class WebRequestResult
{
    [JsonProperty("responseHeader")]
    public ResponseHeader ResponseHeader { get; set; }

    [JsonProperty("response")]
    public Response Response { get; set; }
}

public class Response
{
    [JsonProperty("numFound")]
    public long NumFound { get; set; }

    [JsonProperty("start")]
    public long Start { get; set; }

    [JsonProperty("docs")]
    public List<Doc> Docs { get; set; }
}

public class Doc
{
    [JsonProperty("tstamp")]
    public DateTimeOffset Tstamp { get; set; }

    [JsonProperty("digest")]
    public string Digest { get; set; }

    [JsonProperty("boost")]
    public double Boost { get; set; }

    [JsonProperty("id")]
    public Uri Id { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("url")]
    public Uri Url { get; set; }

    [JsonProperty("content")]
    public string Content { get; set; }

    [JsonProperty("_version_")]
    public double Version { get; set; }
}

public class ResponseHeader
{
    [JsonProperty("status")]
    public long Status { get; set; }

    [JsonProperty("QTime")]
    public long QTime { get; set; }

    [JsonProperty("params")]
    public Params Params { get; set; }
}

public class Params
{
    [JsonProperty("q")]
    public string Q { get; set; }
}

你应该像这样将你的 json 数据解析为C#:

var myData = JsonConvert.DeserializeObject<WebRequestResult>(responseBody);

您可以从 app.quicktype.io 生成您的 C# 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2020-03-29
    • 2020-03-04
    • 2011-05-20
    相关资源
    最近更新 更多