2

I'm working with Highcharts that wants data in the following format and I'm having trouble achieving it:

var data = [
    ["CityName", 1234],
    ["City2", 12123]
]

etc...

So, I'm needing this formatted like the above array and simply can't seem to achieve it.

I've seen this from JSON.NET:

string[,] famousCouples = new string[,]
  {
    { "Adam", "Eve" },
    { "Bonnie", "Clyde" },
    { "Donald", "Daisy" },
    { "Han", "Leia" }
  };

string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);
// [
//   ["Adam", "Eve"],
//   ["Bonnie", "Clyde"],
//   ["Donald", "Daisy"],
//   ["Han", "Leia"]
// ]

That's fine for strings, but how about string and a float together? This is a requirement as one of the numbers needs to aggregate over a list and string[,] really isn't an ideal object to work with.

At the minute I'm using List<Dictionary<string,float> (this would also be the result with a custom object) and the serializer is outputting data as:

var data = [
   {"CityName", 1234}
]

etc.

I may be missing something really simple, but is there any way to achieve this?

0

1 Answer 1

2

I think you probably want object[,]:

object[,] cityNames = new object[,] {
    {"CityName", 1234},
    {"City2", 12345 }
};
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, let me have a crack at that and see if it works :) If so, it'll have saved me for today!
@ChrisDixon: The theory is sound, but we'll see if it survives contact with experimental reality. :-) (I don't have JSON.Net handy, you see.)
@ChrisDixon: Cool! I'm glad that worked! FWIW, it doesn't work with System.Web.Script.Serialization.JavaScriptSerializer. In what I can only consider a flat-out bug in JavaScriptSerializer, it outputs ["CityName", 1234, "City2", 12345] (note that that's just one dimension). Glad JSON.Net gets it right.

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.