Display data to Debug Console

烂漫一生 提交于 2020-01-06 21:10:16

问题


I writing app for UWP

I have this code

var p = await wc.GetProducts(new Dictionary<string, string>() {
            { "orderby", "id" }, { "filter[search]", "1884" }
             });

I try to display data from dictionary like this.

Debug.WriteLine("There");

        Debug.WriteLine(p.products);

But it not works.

How I can display data of dictionary ?


回答1:


Which type does GetProducts() return?

If it's just a Dictionary you can do the following:

foreach(var key in p.Keys)
{
    Debug.WriteLine(key);
}



回答2:


To read a Dictionary --
foreach(KeyValuePair<string,string> kvp in p){
     Console.WriteLine(kvp.Key);
     Console.WriteLine(kvp.Value);
 }
 foreach(string key in p.Keys){
    Console.WriteLine(key);
    Console.WriteLine(p[key]);//value
 }
foreach(string value in p.Values){
    Console.WriteLine(value);
}

But your problem is that P is a class called products:

Product p = new Product()
        {
            name = "test product 8",
            title = "test product 8",
            description = "test product 8",
            price = 8.0M
        };

You would access the properties of p like:

p.name;
p.title;
p.description;
p.price;

Debug.WriteLine(p.name);
Debug.WriteLine(p.title);//etc



回答3:


Using the below extension class (Requires Newtonsoft JSON library) you can get a JSON string of any object either with or without readable formatting.

Using the class to get a readable JSON string;

var p = await wc.GetProducts(new Dictionary<string, string>() {
    { "orderby", "id" }, { "filter[search]", "1884" }
    });

var jsonString = p.ToFormattedJsonString();

Debug.WriteLine(jsonString);

Using the class to get a plain JSON string without format;

var p = await wc.GetProducts(new Dictionary<string, string>() {
    { "orderby", "id" }, { "filter[search]", "1884" }
    });

var jsonString = p.ToJsonString();

Debug.WriteLine(jsonString);

You can also simplfy the above by adding your own extension method such as the below;

public static void ToDebug(this object data)
{
    Debug.WriteLine(data.ToFormattedJsonString());
}

The extension class;

using System.Text;
using Newtonsoft.Json;

namespace System
{
    public static class JsonExtensions
    {
        public static string ToFormattedJsonString(this object obj, bool indentWithTab)
        {
            return indentWithTab
                ? ToFormattedJsonString(obj, "\t")
                : ToFormattedJsonString(obj);
        }

        public static string ToFormattedJsonString(this object obj, string indentString = "  ")
        {
            return FormatJson(obj.ToJsonString(), indentString);
        }

        public static string ToJsonString(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static T DeserializeJsonString<T>(this string jsonString)
        {
            return JsonConvert.DeserializeObject<T>(jsonString);
        }

        private static string FormatJson(string jsonString, string indentString)
        {
            var indent = 0;
            var quoted = false;
            var builder = new StringBuilder();
            for (var i = 0; i < jsonString.Length; i++)
            {
                var character = jsonString[i];
                switch (character)
                {
                    case '{':
                    case '[':
                        builder.Append(character);
                        if (!quoted)
                        {
                            builder.AppendLine();
                            builder.RepeatAppend(++indent, indentString);
                        }
                        break;
                    case '}':
                    case ']':
                        if (!quoted)
                        {
                            builder.AppendLine();
                            builder.RepeatAppend(--indent, indentString);
                        }
                        builder.Append(character);
                        break;
                    case '"':
                        builder.Append(character);
                        bool escaped = false;
                        var index = i;
                        while (index > 0 && jsonString[--index] == '\\')
                            escaped = !escaped;
                        if (!escaped)
                            quoted = !quoted;
                        break;
                    case ',':
                        builder.Append(character);
                        if (!quoted)
                        {
                            builder.AppendLine();
                            builder.RepeatAppend(indent, indentString);
                        }
                        break;
                    case ':':
                        builder.Append(character);
                        if (!quoted)
                            builder.Append(" ");
                        break;
                    default:
                        builder.Append(character);
                        break;
                }
            }
            return builder.ToString();
        }

        public static StringBuilder RepeatAppend(this StringBuilder builder, int count, string format,
            params object[] parameters)
        {
            if (count <= 0 || string.IsNullOrEmpty(format))
                return builder;

            for (int i = 0; i < count; i++)
            {
                builder.AppendFormat(format, parameters);
            }

            return builder;
        }
    }
}


来源:https://stackoverflow.com/questions/39345219/display-data-to-debug-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!