none
Problem with JsonConvert.SerializeObject RRS feed

  • Question

  • Hello,

    I will want to reproduce that

    {
      "userinfo":
        {
          "tag:test1:claim_nationality":null,
        }
      }

    I tried this:

        public class Claim
        {
            public string type { get; set; }
    
            public Object valeur { get; set; }
        }
    
        public class Claims
        {
            [JsonProperty(PropertyName = "userinfo")]
            public Claim listeClaim { get; set; }
        }
    
    
                Claim _claim = new Claim();
                _claim.type = "tag:test1:claim_eid";
                _claim.valeur = null;
    
                Claims _userInfo = new Claims();
    
                _userInfo.listeClaim = _claim;
    
                string json = JsonConvert.SerializeObject(_userInfo, Formatting.None);

    purpose i get this: "{\"userinfo\":{\"type\":\"tag:sixdots.be,2016-06:claim_eid\",\"valeur\":null}}"

    How can I do to remove annotations: type et valeur to have the same as 

    {
      "userinfo":
        {
          "tag:test1:claim_nationality":null,
        }
      }

    Thanks in advance

    Sunday, March 3, 2019 10:09 PM

Answers

  • it's getting closer:

    I get: {"userinfo": {"tag_test1_claim_nationality": null}} but I must have aboslument: "userinfo": {"tag: test1: claim_nationality": null,}}
    • Marked as answer by neotof Monday, March 4, 2019 1:18 PM
    • Unmarked as answer by neotof Monday, March 4, 2019 1:19 PM
    • Edited by neotof Monday, March 4, 2019 1:21 PM After check
    • Marked as answer by neotof Monday, March 4, 2019 1:26 PM
    Monday, March 4, 2019 1:18 PM

All replies

  • Hi neotof,

    Thank you for posting here.

    For your question, I test the code. There is no annotations in the json you get from the code.

    When you output the json data, it is correct.

        Console.WriteLine(json);

     

    >>"{\"userinfo\":{\"type\":\"tag:sixdots.be,2016-06:claim_eid\",\"valeur\":null}}"

    This the format which used to store the data. It would not affect the result.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 5:26 AM
    Moderator
  • > This is the format which used to store the data.

    Just for accuracy's sake, this is not correct.  This is the format used to display the data in the debugger, but those backslashes are NOT stored with the data.

    This is an important point for programmers to understand.  Consider:

        string s = "ab\"cd\\ef";

    That string contains exactly 8 characters.  It will occupy 8 bytes in memory.  However, when displayed in the debugger, it will be presented as the same 12 characters you see above.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, March 4, 2019 6:41 AM
  • Thank you for your answers but my question is not about backslashes

    What I want is to get out exactly this result:

      "userinfo":
        {
          "tag:test1:claim_nationality":null,
        }
      }

    There are no properties names: "type" et "valeurs" as in my result to me above

    thank you

    Monday, March 4, 2019 8:14 AM
  • Hi neotof, 

    I understand your question now. You missing something when you set the value.

    Please try the code below.

     class Program
        {
            public static void Main()
            {
                //Claim _claim = new Claim
                //{
                //    type = "tag:test1:claim_eid",
                //    valeur = null
                //};
    
                //Claims _userInfo = new Claims();
    
                //_userInfo.listeClaim = _claim;
    
                Claims _userInfo = new Claims
                {
                    listeClaim = new Claim
                    {
                        type = "tag:test1:claim_eid",
                        valeur = null
                    }
                };
    
                string json = JsonConvert.SerializeObject(_userInfo, Formatting.None);
            }
    
        }
        public class Claim
        {
            public string type { get; set; }
    
            public Object valeur { get; set; }
        }
    
        public class Claims
        {
            [JsonProperty(PropertyName = "userinfo")]
            public Claim listeClaim { get; set; }
        }
    


    This the json data I get from the code.

    {"userinfo":{"type":"tag:test1:claim_eid","valeur":null}}

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 8:29 AM
    Moderator
  • Hy Wendy,

    No, it's not that yet

    I must get exactly this result:

     "userinfo":
        {
          "tag:test1:claim_nationality":null,
        }
      }

    Monday, March 4, 2019 8:43 AM
  • Hi neotof,

    If you want to get the json in your feedback. You use the wrong classes.

    And please note that "tag:test1:claim_nationality" is not a correct format. Please change it to tag_test1_claim_nationality.

     class Program
        {
            public static void Main()
            {
    
                Claims _userInfo = new Claims
                {
                    listeClaim = new Claim
                    {
                        tag_test1_claim_nationality = null
                    }
                };
    
                string json = JsonConvert.SerializeObject(_userInfo, Formatting.None);
            }
    
        }
    
    
        public class Claims
        {
            [JsonProperty(PropertyName = "userinfo")]
            public Claim listeClaim { get; set; }
        }
    
        public class Claim
        {
            public object tag_test1_claim_nationality { get; set; }
        }
    


    {"userinfo":{"tag_test1_claim_nationality":null}}

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 9:10 AM
    Moderator
  • it's getting closer:

    I get: {"userinfo": {"tag_test1_claim_nationality": null}} but I must have aboslument: "userinfo": {"tag: test1: claim_nationality": null,}}
    • Marked as answer by neotof Monday, March 4, 2019 1:18 PM
    • Unmarked as answer by neotof Monday, March 4, 2019 1:19 PM
    • Edited by neotof Monday, March 4, 2019 1:21 PM After check
    • Marked as answer by neotof Monday, March 4, 2019 1:26 PM
    Monday, March 4, 2019 1:18 PM
  • Ok with JsonProperty(PropertyName

    thank you

    Monday, March 4, 2019 1:27 PM
  • Hi neotof,

    Could you share your solution? Is there something wrong with my code?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 5, 2019 2:28 AM
    Moderator