C# Json.Net Deserialize Json with different “key” parameter










1















I am trying to deserialize JSON using the Json.NET library. JSON which I receive looks like:




"responseHeader":
"zkConnected": true,
"status": 0,
"QTime": 2
,
"suggest":
"mySuggester":
"Ext":
"numFound": 10,
"suggestions": [

"term": "Extra Community",
"weight": 127,
"payload": ""
,

"term": "External Video block",
"weight": 40,
"payload": ""
,

"term": "Migrate Extra",
"weight": 9,
"payload": ""

]






The problem is that the "Ext" that you can see in it is part of the parameter passed in the query string and will always be different. I want to get only the values assigned to the term "term".



I tried something like this, but unfortunately does not works:



public class AutocompleteResultsInfo

public AutocompleteResultsInfo()

this.Suggest = new Suggest();

[JsonProperty("suggest")]
public Suggest Suggest get; set;


public class Suggest

[JsonProperty("mySuggester")]
public MySuggesterElement MySuggesterElement get; set;


public struct MySuggesterElement

public MySuggester MySuggester;
public string JsonString;

public static implicit operator MySuggesterElement(MySuggester MySuggester) =>new MySuggesterElement MySuggester = MySuggester ;
public static implicit operator MySuggesterElement(string String) => new MySuggesterElement JsonString = String ;


public class MySuggester

[JsonProperty("suggestions")]
public Suggestions Suggestions get; set;


public class Suggestions

[JsonProperty("term")]
public string Autocopmplete get; set;


internal class SuggesterElementConverter : JsonConverter

public override bool CanConvert(Type t)

return t == typeof(MySuggesterElement)

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

switch (reader.TokenType)

case JsonToken.String:
case JsonToken.Date:
var stringValue = serializer.Deserialize<string>(reader);
return new MySuggesterElement JsonString = stringValue ;
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<MySuggester>(reader);
return new MySuggesterElement MySuggester = objectValue ;

throw new Exception("Cannot unmarshal type MySuggesterElement");


public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)

var value = (MySuggesterElement)untypedValue;
if (value.JsonString != null)

serializer.Serialize(writer, value.JsonString);
return;

if (value.MySuggester != null)

serializer.Serialize(writer, value.MySuggester);
return;

throw new Exception("Cannot marshal type CollationElements");


public static readonly SuggesterElementConverter Singleton = new SuggesterElementConverter();


public class AutocompleteConverter

public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings

MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =

SuggesterElementConverter.Singleton

;


var results = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(resultJson, AutocompleteConverter.Settings);


Many thanks for your help.



Kind Regerds,
Wojciech










share|improve this question




























    1















    I am trying to deserialize JSON using the Json.NET library. JSON which I receive looks like:




    "responseHeader":
    "zkConnected": true,
    "status": 0,
    "QTime": 2
    ,
    "suggest":
    "mySuggester":
    "Ext":
    "numFound": 10,
    "suggestions": [

    "term": "Extra Community",
    "weight": 127,
    "payload": ""
    ,

    "term": "External Video block",
    "weight": 40,
    "payload": ""
    ,

    "term": "Migrate Extra",
    "weight": 9,
    "payload": ""

    ]






    The problem is that the "Ext" that you can see in it is part of the parameter passed in the query string and will always be different. I want to get only the values assigned to the term "term".



    I tried something like this, but unfortunately does not works:



    public class AutocompleteResultsInfo

    public AutocompleteResultsInfo()

    this.Suggest = new Suggest();

    [JsonProperty("suggest")]
    public Suggest Suggest get; set;


    public class Suggest

    [JsonProperty("mySuggester")]
    public MySuggesterElement MySuggesterElement get; set;


    public struct MySuggesterElement

    public MySuggester MySuggester;
    public string JsonString;

    public static implicit operator MySuggesterElement(MySuggester MySuggester) =>new MySuggesterElement MySuggester = MySuggester ;
    public static implicit operator MySuggesterElement(string String) => new MySuggesterElement JsonString = String ;


    public class MySuggester

    [JsonProperty("suggestions")]
    public Suggestions Suggestions get; set;


    public class Suggestions

    [JsonProperty("term")]
    public string Autocopmplete get; set;


    internal class SuggesterElementConverter : JsonConverter

    public override bool CanConvert(Type t)

    return t == typeof(MySuggesterElement)

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

    switch (reader.TokenType)

    case JsonToken.String:
    case JsonToken.Date:
    var stringValue = serializer.Deserialize<string>(reader);
    return new MySuggesterElement JsonString = stringValue ;
    case JsonToken.StartObject:
    var objectValue = serializer.Deserialize<MySuggester>(reader);
    return new MySuggesterElement MySuggester = objectValue ;

    throw new Exception("Cannot unmarshal type MySuggesterElement");


    public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)

    var value = (MySuggesterElement)untypedValue;
    if (value.JsonString != null)

    serializer.Serialize(writer, value.JsonString);
    return;

    if (value.MySuggester != null)

    serializer.Serialize(writer, value.MySuggester);
    return;

    throw new Exception("Cannot marshal type CollationElements");


    public static readonly SuggesterElementConverter Singleton = new SuggesterElementConverter();


    public class AutocompleteConverter

    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings

    MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
    DateParseHandling = DateParseHandling.None,
    Converters =

    SuggesterElementConverter.Singleton

    ;


    var results = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(resultJson, AutocompleteConverter.Settings);


    Many thanks for your help.



    Kind Regerds,
    Wojciech










    share|improve this question


























      1












      1








      1








      I am trying to deserialize JSON using the Json.NET library. JSON which I receive looks like:




      "responseHeader":
      "zkConnected": true,
      "status": 0,
      "QTime": 2
      ,
      "suggest":
      "mySuggester":
      "Ext":
      "numFound": 10,
      "suggestions": [

      "term": "Extra Community",
      "weight": 127,
      "payload": ""
      ,

      "term": "External Video block",
      "weight": 40,
      "payload": ""
      ,

      "term": "Migrate Extra",
      "weight": 9,
      "payload": ""

      ]






      The problem is that the "Ext" that you can see in it is part of the parameter passed in the query string and will always be different. I want to get only the values assigned to the term "term".



      I tried something like this, but unfortunately does not works:



      public class AutocompleteResultsInfo

      public AutocompleteResultsInfo()

      this.Suggest = new Suggest();

      [JsonProperty("suggest")]
      public Suggest Suggest get; set;


      public class Suggest

      [JsonProperty("mySuggester")]
      public MySuggesterElement MySuggesterElement get; set;


      public struct MySuggesterElement

      public MySuggester MySuggester;
      public string JsonString;

      public static implicit operator MySuggesterElement(MySuggester MySuggester) =>new MySuggesterElement MySuggester = MySuggester ;
      public static implicit operator MySuggesterElement(string String) => new MySuggesterElement JsonString = String ;


      public class MySuggester

      [JsonProperty("suggestions")]
      public Suggestions Suggestions get; set;


      public class Suggestions

      [JsonProperty("term")]
      public string Autocopmplete get; set;


      internal class SuggesterElementConverter : JsonConverter

      public override bool CanConvert(Type t)

      return t == typeof(MySuggesterElement)

      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

      switch (reader.TokenType)

      case JsonToken.String:
      case JsonToken.Date:
      var stringValue = serializer.Deserialize<string>(reader);
      return new MySuggesterElement JsonString = stringValue ;
      case JsonToken.StartObject:
      var objectValue = serializer.Deserialize<MySuggester>(reader);
      return new MySuggesterElement MySuggester = objectValue ;

      throw new Exception("Cannot unmarshal type MySuggesterElement");


      public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)

      var value = (MySuggesterElement)untypedValue;
      if (value.JsonString != null)

      serializer.Serialize(writer, value.JsonString);
      return;

      if (value.MySuggester != null)

      serializer.Serialize(writer, value.MySuggester);
      return;

      throw new Exception("Cannot marshal type CollationElements");


      public static readonly SuggesterElementConverter Singleton = new SuggesterElementConverter();


      public class AutocompleteConverter

      public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings

      MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
      DateParseHandling = DateParseHandling.None,
      Converters =

      SuggesterElementConverter.Singleton

      ;


      var results = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(resultJson, AutocompleteConverter.Settings);


      Many thanks for your help.



      Kind Regerds,
      Wojciech










      share|improve this question
















      I am trying to deserialize JSON using the Json.NET library. JSON which I receive looks like:




      "responseHeader":
      "zkConnected": true,
      "status": 0,
      "QTime": 2
      ,
      "suggest":
      "mySuggester":
      "Ext":
      "numFound": 10,
      "suggestions": [

      "term": "Extra Community",
      "weight": 127,
      "payload": ""
      ,

      "term": "External Video block",
      "weight": 40,
      "payload": ""
      ,

      "term": "Migrate Extra",
      "weight": 9,
      "payload": ""

      ]






      The problem is that the "Ext" that you can see in it is part of the parameter passed in the query string and will always be different. I want to get only the values assigned to the term "term".



      I tried something like this, but unfortunately does not works:



      public class AutocompleteResultsInfo

      public AutocompleteResultsInfo()

      this.Suggest = new Suggest();

      [JsonProperty("suggest")]
      public Suggest Suggest get; set;


      public class Suggest

      [JsonProperty("mySuggester")]
      public MySuggesterElement MySuggesterElement get; set;


      public struct MySuggesterElement

      public MySuggester MySuggester;
      public string JsonString;

      public static implicit operator MySuggesterElement(MySuggester MySuggester) =>new MySuggesterElement MySuggester = MySuggester ;
      public static implicit operator MySuggesterElement(string String) => new MySuggesterElement JsonString = String ;


      public class MySuggester

      [JsonProperty("suggestions")]
      public Suggestions Suggestions get; set;


      public class Suggestions

      [JsonProperty("term")]
      public string Autocopmplete get; set;


      internal class SuggesterElementConverter : JsonConverter

      public override bool CanConvert(Type t)

      return t == typeof(MySuggesterElement)

      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

      switch (reader.TokenType)

      case JsonToken.String:
      case JsonToken.Date:
      var stringValue = serializer.Deserialize<string>(reader);
      return new MySuggesterElement JsonString = stringValue ;
      case JsonToken.StartObject:
      var objectValue = serializer.Deserialize<MySuggester>(reader);
      return new MySuggesterElement MySuggester = objectValue ;

      throw new Exception("Cannot unmarshal type MySuggesterElement");


      public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)

      var value = (MySuggesterElement)untypedValue;
      if (value.JsonString != null)

      serializer.Serialize(writer, value.JsonString);
      return;

      if (value.MySuggester != null)

      serializer.Serialize(writer, value.MySuggester);
      return;

      throw new Exception("Cannot marshal type CollationElements");


      public static readonly SuggesterElementConverter Singleton = new SuggesterElementConverter();


      public class AutocompleteConverter

      public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings

      MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
      DateParseHandling = DateParseHandling.None,
      Converters =

      SuggesterElementConverter.Singleton

      ;


      var results = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(resultJson, AutocompleteConverter.Settings);


      Many thanks for your help.



      Kind Regerds,
      Wojciech







      c# json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 16:49









      marc_s

      582k13011231269




      582k13011231269










      asked Nov 15 '18 at 15:22









      Wojciech SewerynWojciech Seweryn

      304




      304






















          3 Answers
          3






          active

          oldest

          votes


















          3














          You could decode the "mySuggester" as a dictionary:
          public class Suggest



          public class Suggest

          [JsonProperty("mySuggester")]
          public Dictionary<string, MySuggester> MySuggester get; set;



          Then you'll be able to access the suggestions with the query string parameter:



          var variablePropertyName = "Ext";

          var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);

          var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;


          if you don't know the property name you could also look it up in the dictionary:



          var variablePropertyName = result.Suggest.MySuggester.Keys.First();


          Working example:
          https://dotnetfiddle.net/GIKwLs






          share|improve this answer






























            1














            If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:



            private static IEnumerable<string> GetTerms(string json)

            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))

            while (reader.Read())

            if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))

            string term = reader.ReadAsString();

            yield return term;






            Using the code:



            string json = @"
            ""responseHeader"":
            ""zkConnected"": true,
            ""status"": 0,
            ""QTime"": 2
            ,
            ""suggest"":
            ""mySuggester"":
            ""Ext"":
            ""numFound"": 10,
            ""suggestions"": [

            ""term"": ""Extra Community"",
            ""weight"": 127,
            ""payload"": """"
            ,

            ""term"": ""External Video block"",
            ""weight"": 40,
            ""payload"": """"
            ,

            ""term"": ""Migrate Extra"",
            ""weight"": 9,
            ""payload"": """"

            ]



            ";

            IEnumerable<string> terms = GetTerms(json);

            foreach (string term in terms)

            Console.WriteLine(term);






            share|improve this answer






























              1














              If you only need the object containing the term, and nothing else,
              you could work with the JSON values directly by using the JObject interface in JSON.Net.



              var parsed = JObject.Parse(jsonString);
              var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
              .Descendants()
              .OfType<JObject>()
              .Where(x => x.ContainsKey("term"));

              var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(@.term)]")
              .Cast<JObject>();





              share|improve this answer






















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322621%2fc-sharp-json-net-deserialize-json-with-different-key-parameter%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                You could decode the "mySuggester" as a dictionary:
                public class Suggest



                public class Suggest

                [JsonProperty("mySuggester")]
                public Dictionary<string, MySuggester> MySuggester get; set;



                Then you'll be able to access the suggestions with the query string parameter:



                var variablePropertyName = "Ext";

                var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);

                var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;


                if you don't know the property name you could also look it up in the dictionary:



                var variablePropertyName = result.Suggest.MySuggester.Keys.First();


                Working example:
                https://dotnetfiddle.net/GIKwLs






                share|improve this answer



























                  3














                  You could decode the "mySuggester" as a dictionary:
                  public class Suggest



                  public class Suggest

                  [JsonProperty("mySuggester")]
                  public Dictionary<string, MySuggester> MySuggester get; set;



                  Then you'll be able to access the suggestions with the query string parameter:



                  var variablePropertyName = "Ext";

                  var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);

                  var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;


                  if you don't know the property name you could also look it up in the dictionary:



                  var variablePropertyName = result.Suggest.MySuggester.Keys.First();


                  Working example:
                  https://dotnetfiddle.net/GIKwLs






                  share|improve this answer

























                    3












                    3








                    3







                    You could decode the "mySuggester" as a dictionary:
                    public class Suggest



                    public class Suggest

                    [JsonProperty("mySuggester")]
                    public Dictionary<string, MySuggester> MySuggester get; set;



                    Then you'll be able to access the suggestions with the query string parameter:



                    var variablePropertyName = "Ext";

                    var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);

                    var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;


                    if you don't know the property name you could also look it up in the dictionary:



                    var variablePropertyName = result.Suggest.MySuggester.Keys.First();


                    Working example:
                    https://dotnetfiddle.net/GIKwLs






                    share|improve this answer













                    You could decode the "mySuggester" as a dictionary:
                    public class Suggest



                    public class Suggest

                    [JsonProperty("mySuggester")]
                    public Dictionary<string, MySuggester> MySuggester get; set;



                    Then you'll be able to access the suggestions with the query string parameter:



                    var variablePropertyName = "Ext";

                    var result = JsonConvert.DeserializeObject<AutocompleteResultsInfo>(_json);

                    var suggestions = result.Suggest.MySuggester[variablePropertyName].Suggestions;


                    if you don't know the property name you could also look it up in the dictionary:



                    var variablePropertyName = result.Suggest.MySuggester.Keys.First();


                    Working example:
                    https://dotnetfiddle.net/GIKwLs







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 15:48









                    Josef FazekasJosef Fazekas

                    30037




                    30037























                        1














                        If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:



                        private static IEnumerable<string> GetTerms(string json)

                        using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))

                        while (reader.Read())

                        if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))

                        string term = reader.ReadAsString();

                        yield return term;






                        Using the code:



                        string json = @"
                        ""responseHeader"":
                        ""zkConnected"": true,
                        ""status"": 0,
                        ""QTime"": 2
                        ,
                        ""suggest"":
                        ""mySuggester"":
                        ""Ext"":
                        ""numFound"": 10,
                        ""suggestions"": [

                        ""term"": ""Extra Community"",
                        ""weight"": 127,
                        ""payload"": """"
                        ,

                        ""term"": ""External Video block"",
                        ""weight"": 40,
                        ""payload"": """"
                        ,

                        ""term"": ""Migrate Extra"",
                        ""weight"": 9,
                        ""payload"": """"

                        ]



                        ";

                        IEnumerable<string> terms = GetTerms(json);

                        foreach (string term in terms)

                        Console.WriteLine(term);






                        share|improve this answer



























                          1














                          If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:



                          private static IEnumerable<string> GetTerms(string json)

                          using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))

                          while (reader.Read())

                          if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))

                          string term = reader.ReadAsString();

                          yield return term;






                          Using the code:



                          string json = @"
                          ""responseHeader"":
                          ""zkConnected"": true,
                          ""status"": 0,
                          ""QTime"": 2
                          ,
                          ""suggest"":
                          ""mySuggester"":
                          ""Ext"":
                          ""numFound"": 10,
                          ""suggestions"": [

                          ""term"": ""Extra Community"",
                          ""weight"": 127,
                          ""payload"": """"
                          ,

                          ""term"": ""External Video block"",
                          ""weight"": 40,
                          ""payload"": """"
                          ,

                          ""term"": ""Migrate Extra"",
                          ""weight"": 9,
                          ""payload"": """"

                          ]



                          ";

                          IEnumerable<string> terms = GetTerms(json);

                          foreach (string term in terms)

                          Console.WriteLine(term);






                          share|improve this answer

























                            1












                            1








                            1







                            If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:



                            private static IEnumerable<string> GetTerms(string json)

                            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))

                            while (reader.Read())

                            if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))

                            string term = reader.ReadAsString();

                            yield return term;






                            Using the code:



                            string json = @"
                            ""responseHeader"":
                            ""zkConnected"": true,
                            ""status"": 0,
                            ""QTime"": 2
                            ,
                            ""suggest"":
                            ""mySuggester"":
                            ""Ext"":
                            ""numFound"": 10,
                            ""suggestions"": [

                            ""term"": ""Extra Community"",
                            ""weight"": 127,
                            ""payload"": """"
                            ,

                            ""term"": ""External Video block"",
                            ""weight"": 40,
                            ""payload"": """"
                            ,

                            ""term"": ""Migrate Extra"",
                            ""weight"": 9,
                            ""payload"": """"

                            ]



                            ";

                            IEnumerable<string> terms = GetTerms(json);

                            foreach (string term in terms)

                            Console.WriteLine(term);






                            share|improve this answer













                            If you don't need to deserialize the whole json string you can use a JsonTextReader. Example:



                            private static IEnumerable<string> GetTerms(string json)

                            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))

                            while (reader.Read())

                            if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("term"))

                            string term = reader.ReadAsString();

                            yield return term;






                            Using the code:



                            string json = @"
                            ""responseHeader"":
                            ""zkConnected"": true,
                            ""status"": 0,
                            ""QTime"": 2
                            ,
                            ""suggest"":
                            ""mySuggester"":
                            ""Ext"":
                            ""numFound"": 10,
                            ""suggestions"": [

                            ""term"": ""Extra Community"",
                            ""weight"": 127,
                            ""payload"": """"
                            ,

                            ""term"": ""External Video block"",
                            ""weight"": 40,
                            ""payload"": """"
                            ,

                            ""term"": ""Migrate Extra"",
                            ""weight"": 9,
                            ""payload"": """"

                            ]



                            ";

                            IEnumerable<string> terms = GetTerms(json);

                            foreach (string term in terms)

                            Console.WriteLine(term);







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 15:39









                            Rui JarimbaRui Jarimba

                            7,22573460




                            7,22573460





















                                1














                                If you only need the object containing the term, and nothing else,
                                you could work with the JSON values directly by using the JObject interface in JSON.Net.



                                var parsed = JObject.Parse(jsonString);
                                var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
                                .Descendants()
                                .OfType<JObject>()
                                .Where(x => x.ContainsKey("term"));

                                var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(@.term)]")
                                .Cast<JObject>();





                                share|improve this answer



























                                  1














                                  If you only need the object containing the term, and nothing else,
                                  you could work with the JSON values directly by using the JObject interface in JSON.Net.



                                  var parsed = JObject.Parse(jsonString);
                                  var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
                                  .Descendants()
                                  .OfType<JObject>()
                                  .Where(x => x.ContainsKey("term"));

                                  var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(@.term)]")
                                  .Cast<JObject>();





                                  share|improve this answer

























                                    1












                                    1








                                    1







                                    If you only need the object containing the term, and nothing else,
                                    you could work with the JSON values directly by using the JObject interface in JSON.Net.



                                    var parsed = JObject.Parse(jsonString);
                                    var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
                                    .Descendants()
                                    .OfType<JObject>()
                                    .Where(x => x.ContainsKey("term"));

                                    var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(@.term)]")
                                    .Cast<JObject>();





                                    share|improve this answer













                                    If you only need the object containing the term, and nothing else,
                                    you could work with the JSON values directly by using the JObject interface in JSON.Net.



                                    var parsed = JObject.Parse(jsonString);
                                    var usingLinq = (parsed["suggest"]["mySuggester"] as JObject)
                                    .Descendants()
                                    .OfType<JObject>()
                                    .Where(x => x.ContainsKey("term"));

                                    var usingJsonPath = parsed.SelectTokens("$.suggest.mySuggester.*.*[?(@.term)]")
                                    .Cast<JObject>();






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 15 '18 at 16:16









                                    gnudgnud

                                    62.3k55070




                                    62.3k55070



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322621%2fc-sharp-json-net-deserialize-json-with-different-key-parameter%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        這個網誌中的熱門文章

                                        Barbados

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        Node.js Script on GitHub Pages or Amazon S3