0

I'm looking to construct a C# object to produce the following JSON array:

["Key":"SomeKeyValue", "Key":"SomeKeyValue"]

Using a list gives me the array I'm after, but with a object name in the resulting JSON:

Public Class SomeOtherClass
{
    public string Key {get; set;}
}

Public Class SomeObject
{
    public List<SomeOtherClass> PropertyName { get; set; }
}

// JSON
{"PropertyName":[ {"Key":"SomeValue"} ] }

I need to drop the name of the object in the resulting JSON. Using a Dictionary<string,string> gives me what I'm after but with the Object syntax {} in the JSON instead if the Array []:

{
  ...
}

Trying to avoid custom serializer and contracts here if possible. I'm sure that NewtonSoft must have some magic attribute here?

Found a few related questions on the net, but none that resolves removing the property name. Any suggestions?

4
  • You can't produce this JSON array because it isn't even a valid JSON array. Commented Aug 10, 2019 at 19:25
  • converting this example to Dictionary makes the two "Key" values collide, so I think converting to a List might be better. see: stackoverflow.com/questions/39999420/… Commented Aug 10, 2019 at 19:25
  • It needs to match the XML version: <ClassName> <!--Zero or more repetitions:--> <key>value</key> </ClassName> Commented Aug 10, 2019 at 19:43
  • JSON arrays do not have keys, only values. See json.org Commented Aug 10, 2019 at 20:42

1 Answer 1

0

From looking at a XML to JSON converter, the conversion from the XML:

<ClassName>
    <!--Zero or more repetitions:-->
    <key>value1</key>
    <key>value2</key>
    <key>value3</key>
</ClassName>

has the JSON equivalent of:

"ClassName": {
    "key": [
        "value1",
        "value2",
        "value3"
    ]
}

Using this for now until I can test it on more 3rd party systems that I need to integrate with.

Sign up to request clarification or add additional context in comments.

Comments

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.