2

I am using json.net to convert currency rates to json.

The C# entity got a Name and a Value, where Name is USD, GBP and so one, and Value the currencyrate.

I don't know the index of the different currencies, so in javascript I want to get a currency by saying var a = obj["USD"]; insead of looping through an array and find array[i].name == "USD". The default output of JsonConvert.SerializeObject(currencyList); is:

[
    {"name": "one",   "pId": "foo1", "cId": "bar1"},
    {"name": "two",   "pId": "foo2", "cId": "bar2"},
    {"name": "three", "pId": "foo3", "cId": "bar3"}
]

I would however like something like:

{
    "one":   {"pId": "foo1", "cId": "bar1"},
    "two":   {"pId": "foo2", "cId": "bar2"},
    "three": {"pId": "foo3", "cId": "bar3"}
}

Is this possible to acheieve with json.net, or do i need to write my own parser?

1 Answer 1

3

You have to create a dictionary:

Dictionary<String, DataType> dictionary = list.ToDictionary(d => d.name);

Replace DataType by the type of your entity.

Then, you can serialize the dictionary using json.

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.