zoukankan      html  css  js  c++  java
  • Json 格式化显示

    ---

    public string Process(string inputText)
    {
    bool escaped = false;
    bool inquotes = false;
    int column = 0;
    int indentation = 0;
    Stack<int> indentations = new Stack<int>();
    int TABBING = 8;
    StringBuilder sb = new StringBuilder();
    foreach (char x in inputText)
    {
    sb.Append(x);
    column++;
    if (escaped)
    {
    escaped = false;
    }
    else
    {
    if (x == '\')
    {
    escaped = true;
    }
    else if (x == '"')
    {
    inquotes = !inquotes;
    }
    else if (!inquotes)
    {
    if (x == ',')
    {
    // if we see a comma, go to next line, and indent to the same depth
    sb.Append(" ");
    column = 0;
    for (int i = 0; i < indentation; i++)
    {
    sb.Append(" ");
    column++;
    }
    }
    else if (x == '[' || x == '{')
    {
    // if we open a bracket or brace, indent further (push on stack)
    indentations.Push(indentation);
    indentation = column;
    }
    else if (x == ']' || x == '}')
    {
    // if we close a bracket or brace, undo one level of indent (pop)
    indentation = indentations.Pop();
    }
    else if (x == ':')
    {
    // if we see a colon, add spaces until we get to the next
    // tab stop, but without using tab characters!
    while ((column % TABBING) != 0)
    {
    sb.Append(' ');
    column++;
    }
    }
    }
    }
    }
    return sb.ToString();
    }

    public static string FormatOutput(string jsonString)
    {
    var stringBuilder = new StringBuilder();

    bool escaping = false;
    bool inQuotes = false;
    int indentation = 0;

    foreach (char character in jsonString)
    {
    if (escaping)
    {
    escaping = false;
    stringBuilder.Append(character);
    }
    else
    {
    if (character == '\')
    {
    escaping = true;
    stringBuilder.Append(character);
    }
    else if (character == '"')
    {
    inQuotes = !inQuotes;
    stringBuilder.Append(character);
    }
    else if (!inQuotes)
    {
    if (character == ',')
    {
    stringBuilder.Append(character);
    stringBuilder.Append(" ");
    stringBuilder.Append(' ', indentation);
    }
    else if (character == '[' || character == '{')
    {
    stringBuilder.Append(character);
    stringBuilder.Append(" ");
    stringBuilder.Append(' ', ++indentation);
    }
    else if (character == ']' || character == '}')
    {
    stringBuilder.Append(" ");
    stringBuilder.Append(' ', --indentation);
    stringBuilder.Append(character);
    }
    else if (character == ':')
    {
    stringBuilder.Append(character);
    stringBuilder.Append(' ');
    }
    else
    {
    stringBuilder.Append(character);
    }
    }
    else
    {
    stringBuilder.Append(character);
    }
    }
    }

    return stringBuilder.ToString();
    }

  • 相关阅读:
    推荐系统中的注意力机制——阿里深度兴趣网络(DIN)
    负样本采样及bias校准、ctr平滑
    todo提纲
    漫谈深度学习时代点击率预估技术进展 &&深度学习在推荐系统上的发展
    对数据分析的一点思考
    计算广告中常用深度学习网络
    pandas常见函数详细使用
    ann搜索算法(Approximate Nearest Neighbor)
    LintCode翻转字符串问题
    python常用库
  • 原文地址:https://www.cnblogs.com/haobadea/p/5620800.html
Copyright © 2011-2022 走看看