28

I need to get:

public class Package
{
    public Package()
    {
        name = "";
        type = new List<Dictionary<string, string>>();
    }

    public string name { get; set; }
    public List<Dictionary<string, string>> type { get; set; }
}

into:

{
    "name":"package_name",
    "type":
    {
        "http://random.url.as.key":"random/value"
    }
}

with:

Package package = new Package();
package.name = "package_name";
package.type.Add(new Dictionary<string, string>() { { "http://random.url.as.key", "random/value" } });

I get:

{
    "name":"package_name",
    "type":
    [
        [
            {
                "Key":"http:\/\/random.url.as.key",
                "Value":"random\/value"
            }
        ]
    ]
}

while, with:

var package = new
{
    name = "package_name",
    type = new
    {
        http_random_url_as_key = "random/value"
    }
};

I get:

{
    "name":"package_name",
    "type":
    {
        "http_random_url_as_key":"random/value"
    }
}

I can't get the obsure http://random.url.as.key that I need. I have tried using JavaScriptSerializer, DataContractJsonSerializer, and Custom Convertor for Json.NET, all with limited success/shortcomings.

There must be a better way/something I'm overlooking to get a simple JSON Object over the wire!

4
  • what are you using to serialize into JSON? Commented Feb 26, 2011 at 3:43
  • 3
    You first Package class won't compile. And are you sure the field type is a List<Dictionary<string,string>>? It seems that you want it to perform like a Dictionary<string,string> when converting to JSON. Commented Feb 26, 2011 at 3:46
  • Use this to serialize the .net code to JSON and revert back. james.newtonking.com/projects/json-net.aspx Commented Feb 26, 2011 at 5:03
  • This post can help you in your problem: stackoverflow.com/a/36223340/1442180 Commented Mar 25, 2016 at 17:10

1 Answer 1

47

Well, first off, for the first example, what you basically have is a list of collections of KeyValuePair<string,string> objects.

So, the reason that it gets converted to the JSON shown is this:

{
    "name":"package_name",
    "type":
    [ // List<Dictionary<string,string>>
        [ // Dictionary<string,string>, a list of KeyValuePair<string,string> objects
            { // KeyValuePair<string,string> object 
                "Key":"http:\/\/random.url.as.key",
                "Value":"random\/value"
            }
        ]
    ]
}

As far as your second example, you are creating a dynamic object, containing a dynamic object, and each of the object's fields are what are providing the key value.

So, I would suggest ditching the outer List<> around the Dictionary<string,string>, then make use of the Newtonsoft.Json.Converters.KeyValuePairConverter object in the JSON.Net library when doing your serialization:

string json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ) );

Hope that helps.

EDIT

Okay, so I figured I should give a more concrete example

public class Package
{
    public Package()
    {
        name = "";
        type = new Dictionary<string, string>();
    }

    public string name { get; set; }
    public Dictionary<string, string> type { get; set; }
}

Package package = new Package();
package.name = "package_name";
package.type.Add("http://random.url.as.key", "random/value");
string json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ) );

This will get you the output

{
    "name":"package_name",
    "type":
    {
        "http://random.url.as.key":"random/value"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If I want to have both wcf and json endpoints, how would this code fit?
Usage of KeyValuePairConverter is exactly what i need. Thanks!

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.