Output json property name with dash












0















I am using a third party API which is like this below. It uses Json serializer to output



public Output(string name, object value);



I have to place the following json string in my output file from C#



Output("somename", new {my-name : "somevalue"})



The issue is C# does not allow identifiers with dash (-). How do I achieve this ?



Tried putting raw value like below but it adds back slash () to the file output which is not going very well.



Output("somename", @"{""my-name"":""somevalue""}")



Any thoughts ?










share|improve this question























  • What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

    – Michael Randall
    Nov 21 '18 at 4:05






  • 1





    Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

    – dbc
    Nov 21 '18 at 4:10











  • Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

    – dbc
    Nov 21 '18 at 4:14













  • @dbc put it as answer. That works.

    – Frank Q.
    Nov 21 '18 at 16:33
















0















I am using a third party API which is like this below. It uses Json serializer to output



public Output(string name, object value);



I have to place the following json string in my output file from C#



Output("somename", new {my-name : "somevalue"})



The issue is C# does not allow identifiers with dash (-). How do I achieve this ?



Tried putting raw value like below but it adds back slash () to the file output which is not going very well.



Output("somename", @"{""my-name"":""somevalue""}")



Any thoughts ?










share|improve this question























  • What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

    – Michael Randall
    Nov 21 '18 at 4:05






  • 1





    Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

    – dbc
    Nov 21 '18 at 4:10











  • Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

    – dbc
    Nov 21 '18 at 4:14













  • @dbc put it as answer. That works.

    – Frank Q.
    Nov 21 '18 at 16:33














0












0








0








I am using a third party API which is like this below. It uses Json serializer to output



public Output(string name, object value);



I have to place the following json string in my output file from C#



Output("somename", new {my-name : "somevalue"})



The issue is C# does not allow identifiers with dash (-). How do I achieve this ?



Tried putting raw value like below but it adds back slash () to the file output which is not going very well.



Output("somename", @"{""my-name"":""somevalue""}")



Any thoughts ?










share|improve this question














I am using a third party API which is like this below. It uses Json serializer to output



public Output(string name, object value);



I have to place the following json string in my output file from C#



Output("somename", new {my-name : "somevalue"})



The issue is C# does not allow identifiers with dash (-). How do I achieve this ?



Tried putting raw value like below but it adds back slash () to the file output which is not going very well.



Output("somename", @"{""my-name"":""somevalue""}")



Any thoughts ?







c# .net json json.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 4:00









Frank Q.Frank Q.

1,55372947




1,55372947













  • What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

    – Michael Randall
    Nov 21 '18 at 4:05






  • 1





    Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

    – dbc
    Nov 21 '18 at 4:10











  • Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

    – dbc
    Nov 21 '18 at 4:14













  • @dbc put it as answer. That works.

    – Frank Q.
    Nov 21 '18 at 16:33



















  • What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

    – Michael Randall
    Nov 21 '18 at 4:05






  • 1





    Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

    – dbc
    Nov 21 '18 at 4:10











  • Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

    – dbc
    Nov 21 '18 at 4:14













  • @dbc put it as answer. That works.

    – Frank Q.
    Nov 21 '18 at 16:33

















What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

– Michael Randall
Nov 21 '18 at 4:05





What third party serializer, i am having a lot of trouble understanding this question, however it could be just me

– Michael Randall
Nov 21 '18 at 4:05




1




1





Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

– dbc
Nov 21 '18 at 4:10





Why not just use a dictionary? new Dictionary<string, string> { { "my-name", "somevalue" } }

– dbc
Nov 21 '18 at 4:10













Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

– dbc
Nov 21 '18 at 4:14







Since you have tagged this json.net, it is probably a duplicate of How can I parse a JSON string that would cause illegal C# identifiers?. Agree?

– dbc
Nov 21 '18 at 4:14















@dbc put it as answer. That works.

– Frank Q.
Nov 21 '18 at 16:33





@dbc put it as answer. That works.

– Frank Q.
Nov 21 '18 at 16:33












1 Answer
1






active

oldest

votes


















2














Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:



Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });


Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.



And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:



Output("somename",
new Dictionary<string, object>
{
{ "my-name", "somevalue" },
{ "my-id", 101 },
{ "my-value", value },
});





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%2f53405088%2foutput-json-property-name-with-dash%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:



    Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });


    Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.



    And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:



    Output("somename",
    new Dictionary<string, object>
    {
    { "my-name", "somevalue" },
    { "my-id", 101 },
    { "my-value", value },
    });





    share|improve this answer






























      2














      Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:



      Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });


      Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.



      And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:



      Output("somename",
      new Dictionary<string, object>
      {
      { "my-name", "somevalue" },
      { "my-id", 101 },
      { "my-value", value },
      });





      share|improve this answer




























        2












        2








        2







        Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:



        Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });


        Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.



        And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:



        Output("somename",
        new Dictionary<string, object>
        {
        { "my-name", "somevalue" },
        { "my-id", 101 },
        { "my-value", value },
        });





        share|improve this answer















        Since you are using an anonymous object, the simplest workaround would be to create a Dictionary<string, string> instead:



        Output("somename", new Dictionary<string, string> { { "my-name", "somevalue" } });


        Serialization of a dictionary as a JSON object in which the dictionary keys are mapped to JSON property names is supported by many .Net JSON serializers including json.net and javascriptserializer and even datacontractjsonserializer when UseSimpleDictionaryFormat = true as noted here.



        And if you have values of different types and are using a serializer that supports arbitrary polymorphism during serialization (Json.NET and JavaScriptSerializer do) you could use a Dictionary<string, object>:



        Output("somename",
        new Dictionary<string, object>
        {
        { "my-name", "somevalue" },
        { "my-id", 101 },
        { "my-value", value },
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 17:53

























        answered Nov 21 '18 at 16:40









        dbcdbc

        55.4k875130




        55.4k875130
































            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%2f53405088%2foutput-json-property-name-with-dash%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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word