Output json property name with dash
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
add a comment |
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
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
add a comment |
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
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
c# .net json json.net
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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 },
});
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 },
});
add a comment |
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 },
});
add a comment |
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 },
});
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 },
});
edited Nov 21 '18 at 17:53
answered Nov 21 '18 at 16:40
dbcdbc
55.4k875130
55.4k875130
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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