Пример #1
0
        private IDictionary <string, object> GetCsvReadyValues(JSONDocument json)
        {
            IDictionary <string, object> valueMap = new HashVector <string, object>();
            IEnumerator e = json.GetEnumerator();

            while (e.MoveNext())
            {
                object obj  = e.Current;
                var    type = obj.GetType();
                if (type.IsGenericType)
                {
                    if (type == typeof(KeyValuePair <string, object>))
                    {
                        var key      = type.GetProperty("Key");
                        var value    = type.GetProperty("Value");
                        var keyObj   = key.GetValue(obj, null);
                        var valueObj = value.GetValue(obj, null);

                        if (valueObj.GetType() == typeof(System.Collections.ArrayList) || valueObj.GetType() == typeof(JSONDocument))
                        {
                            // remove internal qoutes and append to start and end
                            string rem = "\"";
                            rem += valueObj.ToString().Replace("\"", string.Empty);
                            rem += "\"";
                            valueMap.Add(keyObj.ToString(), rem);
                        }
                        else
                        {
                            if (valueObj.GetType() == typeof(string))
                            {
                                var val = new StringBuilder();
                                val.Append("\"");
                                val.Append(valueObj.ToString());
                                val.Append("\"");

                                valueMap.Add(keyObj.ToString(), val.ToString());
                            }
                            else
                            {
                                valueMap.Add(keyObj.ToString(), valueObj.ToString());
                            }
                        }
                    }
                }
            }
            return(valueMap);
        }
Пример #2
0
        /// <summary>
        /// converts json to csv and populates header
        /// </summary>
        /// <param name="json"></param>
        /// <param name="headerPositionMap"></param>
        /// <param name="file"></param>
        private void WriteHeaderRow(JSONDocument json, out IDictionary <int, string> headerPositionMap, StreamWriter file)
        {
            string _delim = ",";

            headerPositionMap = new HashVector <int, string>();
            string      headerString = string.Empty;
            string      valueString  = string.Empty;
            IEnumerator e            = json.GetEnumerator();
            int         position     = 0;

            while (e.MoveNext())
            {
                if (position != 0)
                {
                    headerString += _delim;
                    valueString  += _delim;
                }
                object obj  = e.Current;
                var    type = obj.GetType();
                if (type.IsGenericType)
                {
                    if (type == typeof(KeyValuePair <string, object>))
                    {
                        var key      = type.GetProperty("Key");
                        var value    = type.GetProperty("Value");
                        var keyObj   = key.GetValue(obj, null);
                        var valueObj = value.GetValue(obj, null);

                        headerPositionMap.Add(position, keyObj.ToString());

                        headerString += keyObj.ToString();

                        if (valueObj.GetType() == typeof(System.Collections.ArrayList) || valueObj.GetType() == typeof(JSONDocument))
                        {
                            // remove internal qoutes and append to start and end
                            string rem = "\"";
                            rem         += valueObj.ToString().Replace("\"", string.Empty);
                            rem         += "\"";
                            valueString += rem;
                        }
                        else
                        {
                            if (valueObj.GetType() == typeof(string))
                            {
                                var val = new StringBuilder();
                                val.Append("\"");
                                val.Append(valueObj.ToString());
                                val.Append("\"");

                                valueString += val.ToString();
                            }
                            else
                            {
                                valueString += valueObj.ToString();
                            }
                        }
                        position++;
                    }
                }
            }
            file.WriteLine(headerString);
            file.WriteLine(valueString);
        }