1

I need to return an array of arrays instead of an array of objects for a flot chart.

I can get the following:

data = [{"2012-10": 4140},{"2012-11": 10815},{"2012-12": 10444}];

but need (UPDATE fixed following line):

data = [["2012-10", 4140],["2012-11", 10815],["2012-12", 10444]];

Here is the c# which is parsing inbound json from another source:

public async Task<ActionResult> Chart(string custid)
   {
        //Get the financial results from DRT
        var response = await DominoJSON.getJSON(custid, "drtHistory", "DRT");

        JArray UOVol = new JArray();

        var array = JArray.Parse(response);
        foreach (var token in array)
        {
            JObject o = JObject.Parse(token.ToString());
            
            int uovol = Convert.ToInt32(o["UOVol"]);
            string uodate = o.SelectToken("DATE").ToString();
            JObject UOItem = new JObject(new JProperty(uodate, uovol));
            UOVol.Add(UOItem);
              
        }

       string resultUO = UOVol.ToString();
       
       ViewBag.UOData = resultUO;

        return View();
    }

And the inbound json being parsed:

[
  {
      "DATE":"2012-10",
      "UOVol":4140,
      "FIRev":180,
      "AFRev":692.75,
      "ABRev":2900,
      "OWRev":3791.25,
  },
  {
     
      "DATE":"2012-11",
      "UOVol":10815,
      "FIRev":60,
      "AFRev":170,
      "ABRev":0,
      "OWRev":3037.5,
  },
  {
   
      "DATE":"2012-12",
      "UOVol":10444,
      "FIRev":40,
      "AFRev":514.25,
      "ABRev":1450,
      "OWRev":7500,
  }
]

I can't figure out how to turn the JObjects to arrays. I have tried this with other approaches including using a dictionary and list. Any help or alternate solutions would be helpful. Using VS2013, mvc 5 and EF 6.

3
  • ["2012-10": 4140] is not valid JSON. Commented Dec 13, 2013 at 0:45
  • The JSON you say that you need is invalid -- there can't be colons separating values in an array. Did you mean [["2012-10", 4140],["2012-11", 10815],["2012-12", 10444]]? Commented Dec 13, 2013 at 0:45
  • Yes I meant as you indicated with commas instead of colons. I just copied over incorrectly. Commented Dec 13, 2013 at 1:00

1 Answer 1

3

Try this:

class Program
{
    static void Main(string[] args)
    {
        string jsonIn = @"
        [
            {
                ""DATE"": ""2012-10"",
                ""UOVol"": 4140,
                ""FIRev"": 180,
                ""AFRev"": 692.75,
                ""ABRev"": 2900,
                ""OWRev"": 3791.25
            },
            {
                ""DATE"": ""2012-11"",
                ""UOVol"": 10815,
                ""FIRev"": 60,
                ""AFRev"": 170,
                ""ABRev"": 0,
                ""OWRev"": 3037.5
            },
            {
                ""DATE"": ""2012-12"",
                ""UOVol"": 10444,
                ""FIRev"": 40,
                ""AFRev"": 514.25,
                ""ABRev"": 1450,
                ""OWRev"": 7500
            }
        ]";

        JArray arrayIn = JArray.Parse(jsonIn);
        JArray arrayOut = new JArray();
        foreach (JObject jo in arrayIn.Children<JObject>())
        {
            JArray ja = new JArray();
            ja.Add(jo["DATE"]);
            ja.Add(jo["UOVol"]);
            arrayOut.Add(ja);
        }

        string jsonOut = arrayOut.ToString(Formatting.None);
        Console.WriteLine(jsonOut);
    }
}

Output:

[["2012-10",4140],["2012-11",10815],["2012-12",10444]]
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.