简体   繁体   中英

Parse Json Response Data

I am calling a Solr Apache search url and it turns Json data format. However, when I parse the Json, I receive null data. My Json format is like below:

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"
         .....

so I created dataModel to map the json data format:

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; }
}

my code to retrieve json search results:

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 
}

Where is the problem? BTW, there are a lot of \n line break in the json data. Is that the problem and how to deal with it? Thanks

add json data sample below:

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

The problem is that you have an object in your json namely response and in your c# classes, you have created the property named responseData which won't map because they differ in the name, Either you would have to set the JsonProperty name to response or you should entirely change the name of your property to response . Besides, there is a web application that will correctly parse your json and will generate the C# classes for you relevantly. Here is the code that I have generated for you for the response you have shared:

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; }
}

And you should parse your json data to C# like this:

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

You can generate your C# classes from app.quicktype.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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