1

Quite a subjective question in reality, however I am just trying to explore some options to create a flare.json output for my D3js visualisations.

At the moment I have a JSON structure for D3js as follows:

{
    "name": "Engage Stats",
    "children":
    [
        {
            "name": "Unique Requests by Device",
            "children":
            [
                {"name": "Android", "size": 80},
                {"name": "IOS", "size": 366}
            ]
        },
        {
            "name": "Overall Requests by Device",
            "children":
            [
                {"name": "Android", "size": 2645},
                {"name": "IOS", "size": 11703}
            ]
        },

                .... etc etc

My data is being retrieved from MS-SQL using a stored proc. Obviously one way is to simply read through my dataset (I should have said I am using C#/.NET at the backend) and build up the JSON structure row by row.. however I just wonder if anyone else has had a better/cleaner idea!

I guess because I am using the flare.json format for more of the visualisations, I can create a library to do the heavy lifting - again, just interested in a other perspectives to achieve this.

4

1 回答 1

1

Just for completeness, and to close this question, I ended up going with a good old fashioned way.. not the best I am guessing, however it does the job!

using (SqlDataReader dr = command.ExecuteReader())
                        {
                            //  Check if we have data or not - no need to create the excel file if no data
                            if (!dr.HasRows)
                            {
                                JSONdata = null;
                                return false;
                            }

                            StringBuilder json = new StringBuilder();
                            string LastGroup = null;
                            bool Init = true;

                            json.AppendLine("{");

                            while (dr.Read())
                            {
                                if (Init)
                                {
                                    json.AppendLine("\"name\": \"" + dr["Organisation"] + "\",");
                                    json.AppendLine("\"children\": ");
                                    json.AppendLine("[");
                                }
                                if (LastGroup != dr["GroupIdentifier"].ToString())
                                {
                                    if (Init == true)
                                    {
                                        json.AppendLine("{");
                                        json.AppendLine("\"name\": \"" + dr["GroupIdentifier"] + "\",");
                                        json.AppendLine("\"children\":");
                                        json.AppendLine("[");
                                        Init = false;
                                    }
                                    else
                                    {
                                        var index = json.ToString().LastIndexOf(',');
                                        if (index >= 0)
                                        {
                                            json.Remove(index, 1);
                                        }
                                        json.AppendLine("]");
                                        json.AppendLine("},");
                                        json.AppendLine("{");
                                        json.AppendLine("\"name\": \"" + dr["GroupIdentifier"] + "\",");
                                        json.AppendLine("\"children\":");
                                        json.AppendLine("[");
                                    }
                                    LastGroup = dr["GroupIdentifier"].ToString();
                                }

                                json.AppendLine("{\"name\": \"" + dr["Measure"] + "\", \"size\": " + dr["MeasureValue"].ToString() + "},");
                            }

                            var index2 = json.ToString().LastIndexOf(',');
                            if (index2 >= 0)
                            {
                                json.Remove(index2, 1);
                            }
                            json.AppendLine("]");
                            json.AppendLine("}");

                            json.AppendLine("]");
                            json.AppendLine("}");

                            JSONdata = json.ToString();
                            return true;

                        } // end SqlRdr
于 2013-08-15T09:18:48.600 回答