简体   繁体   中英

Deserializing Server returned String to JSON

I am using the SharePoint REST API to do a search. I am pulling back the JSON results and reading them as follows:

HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(querystring);

endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json; odata=verbose";
endpointRequest.UseDefaultCredentials = true;
HttpWebResponse endpointResponse =(HttpWebResponse)endpointRequest.GetResponse();

Stream webStream = endpointResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
var results = responseReader.ReadToEnd();

This works fine, I can see the results in JSON format. So I created a class from the JSON in VS 2017 and here is the classes created from the JSON (this was done automatically with File=>Paste Special=>Paste JSON As Classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class SharePointRESTResults
    {
        public class Rootobject
        {
            public D d { get; set; }
        }

        public class D
        {
            public Query query { get; set; }
        }

        public class Query
        {
            public __Metadata __metadata { get; set; }
            public int ElapsedTime { get; set; }
            public Primaryqueryresult PrimaryQueryResult { get; set; }
            public Properties1 Properties { get; set; }
            public Secondaryqueryresults SecondaryQueryResults { get; set; }
            public string SpellingSuggestion { get; set; }
            public Triggeredrules TriggeredRules { get; set; }
        }

        public class __Metadata
        {
            public string type { get; set; }
        }

        public class Primaryqueryresult
        {
            public __Metadata1 __metadata { get; set; }
            public Customresults CustomResults { get; set; }
            public string QueryId { get; set; }
            public string QueryRuleId { get; set; }
            public object RefinementResults { get; set; }
            public Relevantresults RelevantResults { get; set; }
            public object SpecialTermResults { get; set; }
        }

        public class __Metadata1
        {
            public string type { get; set; }
        }

        public class Customresults
        {
            public __Metadata2 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata2
        {
            public string type { get; set; }
        }

        public class Relevantresults
        {
            public __Metadata3 __metadata { get; set; }
            public object GroupTemplateId { get; set; }
            public object ItemTemplateId { get; set; }
            public Properties Properties { get; set; }
            public object ResultTitle { get; set; }
            public object ResultTitleUrl { get; set; }
            public int RowCount { get; set; }
            public Table Table { get; set; }
            public int TotalRows { get; set; }
            public int TotalRowsIncludingDuplicates { get; set; }
        }

        public class __Metadata3
        {
            public string type { get; set; }
        }

        public class Properties
        {
            public Result[] results { get; set; }
        }

        public class Result
        {
            public __Metadata4 __metadata { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata4
        {
            public string type { get; set; }
        }

        public class Table
        {
            public __Metadata5 __metadata { get; set; }
            public Rows Rows { get; set; }
        }

        public class __Metadata5
        {
            public string type { get; set; }
        }

        public class Rows
        {
            public Result1[] results { get; set; }
        }

        public class Result1
        {
            public __Metadata6 __metadata { get; set; }
            public Cells Cells { get; set; }
        }

        public class __Metadata6
        {
            public string type { get; set; }
        }

        public class Cells
        {
            public Result2[] results { get; set; }
        }

        public class Result2
        {
            public __Metadata7 __metadata { get; set; }
            public string Key { get; set; }
            public object Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata7
        {
            public string type { get; set; }
        }

        public class Properties1
        {
            public Result3[] results { get; set; }
        }

        public class Result3
        {
            public __Metadata8 __metadata { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata8
        {
            public string type { get; set; }
        }

        public class Secondaryqueryresults
        {
            public __Metadata9 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata9
        {
            public string type { get; set; }
        }

        public class Triggeredrules
        {
            public __Metadata10 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata10
        {
            public string type { get; set; }
        }

    }
}

So I now am trying to deserialize the results as follows:

 SharePointRESTResults resultX = JsonConvert.DeserializeObject<SharePointRESTResults>(results());

The results I need should be in the Result2 Class. The problem I have is that this line is setting resultX = to "ConsoleApplication1.SharePointRESTResults" and nothing more. I looked in the JSON Serializing and Deserializing here: JSON Documentation but still cannot get the results into the class so I can pull out the data. I appreciate any help.

The main points are -

  • The Account Declaration.
  • JsonProperty attribute.

NOTE : If you have an object Which keys can change, Then you need to use a

 Dictionary<string, T> 

. A regular class won't work for that; neither will a

 List<T> 

.

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