解析Json响应数据

时间:2020-05-20 01:44:58

标签: c# asp.net json

我正在调用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
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

问题是您在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#类