0

I am trying to create this nested json string in .NET Core2/C# from model but cant quite wrap my head around it:

{ 
  \"SearchByKeywordRequest\":
   { 
     \"keyword\": \""+transistor+"\", \"records\": 0, \"startingRecord\": 0, \"searchOptions\": \"string\", \"searchWithYourSignUpLanguage\": \"string\" 
   }
 }

I referred to this solution but couldnt get it to format properly for my case

Model

 public class SearchByKeyword
{
    public List<SearchByKeyword> SearchByKeywordRequest { get; set; }
    public string keyword { get; set; }
    public int records { get; set; } = 10;
    public string searchOptions { get; set; } = string.Empty;
    public string searchWithYourSignUpLanguage { get; set; } = string.Empty;
}

Method

public string CreateJson(string transistor) {
        var obj = new SearchByKeyword() {                     
            SearchByKeywordRequest = new List<SearchByKeyword>()
            {
                new SearchByKeyword()
                {
                    keyword = transistor
                }
            }

        };

       return  JsonConvert.SerializeObject(obj);  
    }

this is the string i end up with

"{
  \"SearchByKeywordRequest\":
[ { \"SearchByKeywordRequest\":null, \"keyword\":\"KSA992F\",\"records\":10,\"searchOptions\":\"\",\"searchWithYourSignUpLanguage\":\"\"}],\"keyword\":null,\"records\":10,\"searchOptions\":\"\",\"searchWithYourSignUpLanguage\":\"\"
 }"

Now if I try this in method

    var obj = new List<SearchByKeyword>()
            {
                new SearchByKeyword()
                {
                 keyword = transistor
                }
            };

I get this string

"[{\"SearchByKeywordRequest\":null,\"keyword\":\"KSA992F\",\"records\":10,\"searchOptions\":\"\",\"searchWithYourSignUpLanguage\":\"\"}]"   

Updated Model

    public class SearchByKeyword
{
    [JsonProperty("SearchByKeywordRequest", NullValueHandling = NullValueHandling.Ignore)]
    public SearchByKeyword SearchByKeywordRequest { get; set; }
    public string keyword { get; set; }
    public int records { get; set; } = 10;   
}

Updated Method

       var obj = new
            {
                SearchByKeywordRequest = new SearchByKeyword()
                {
                    keyword = transistor
                }
            };

            string json = JsonConvert.SerializeObject(obj);
4
  • Do not understand what you expect.The first json does not look like a nested json and what the problem with second json? Commented Nov 19, 2019 at 5:55
  • My apologies perhaps it is not nested i'm new to this. The second json doesnt have an open curly bracket after SearchByKeywordRequest, and has a :null. Also the second json is in an array, I am unsure if that matters or not. When I run the json as array in the API UI I get an error. Commented Nov 19, 2019 at 15:38
  • 1
    Since your type of SearchByKeywordRequest is List<SearchByKeyword> and it will certainly be serialized to an array. for the null, it is becaues it has no children data.You could add [JsonProperty("SearchByKeywordRequest", NullValueHandling = NullValueHandling.Ignore)] attribute for SearchByKeywordRequest to bypass it Commented Nov 21, 2019 at 7:22
  • I got desired output thanks to your tips. I posted updated code. Removed the lists as they were not necessary. Commented Dec 2, 2019 at 16:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.