【问题标题】:Parse a JSON Array out of JSON String?从 JSON 字符串中解析一个 JSON 数组?
【发布时间】:2014-12-10 17:42:29
【问题描述】:

在将其标记为重复之前,我想说我有自己的 JsonObject 实现,这就是我在这里寻求指导的原因。

我一直在摸索如何从 JSON 字符串中解析出一个或多个 JSON 数组。我有以下代码:

class JsonObject
{
    public struct JsonProperty
    {
        public String Name { get; set; }
        public String Value { get; set; }
    }

    public Dictionary<String, JsonProperty> Properties;

    public JsonObject()
    {
        Properties = new Dictionary<String, JsonProperty>();
    }

    public JsonObject(String jsonString)
    {
        Properties = new Dictionary<String, JsonProperty>();
        ParseString(jsonString);
    }

    public JsonObject(List<String> properties)
    {
        Properties = new Dictionary<String, JsonProperty>();
        foreach (String s in properties)
        {
            String[] keyvaluepair = s.Split(':');
            JsonProperty prop = new JsonProperty();
            prop.Name = keyvaluepair[0];
            prop.Value = keyvaluepair[1];

            Properties.Add(prop.Name, prop);
        }
    }

    public void AddProperty(String name, String value)
    {
        JsonProperty prop = new JsonProperty();
        prop.Name = name;
        prop.Value = value;

        Properties.Add(prop.Name, prop);
    }

    private void ParseString(String jsonString)
    {
        String[] splitByComma = jsonString.Split(',');
        List<String[]> splitByColon = new List<String[]>();
        foreach (String s in splitByComma)
        {
            String[] split = s.Split(':');
            splitByColon.Add(split);
        }

        for (int i = 0; i < splitByColon.Count; i++)
        {
            for (int j = 0; j < splitByColon[i].Length; j++)
            {
                splitByColon[i][j] = splitByColon[i][j].Replace(",", "");
                splitByColon[i][j] = splitByColon[i][j].Replace("}", "");
                splitByColon[i][j] = splitByColon[i][j].Replace("{", "");
                splitByColon[i][j] = splitByColon[i][j].Replace("\"", "");
                splitByColon[i][j] = splitByColon[i][j].Replace("\\", "");
                splitByColon[i][j] = splitByColon[i][j].Replace(":", "");
            }
        }

        foreach (String[] array in splitByColon)
        {
            JsonProperty p = new JsonProperty();
            p.Name = array[0];
            p.Value = array[1];
            Properties.Add(p.Name, p);
        }
    }

    public override String ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("{");
        int count = 1;
        foreach (KeyValuePair<String, JsonProperty> p in Properties)
        {
            sb.Append("\"" + p.Key.ToString() + "\"");
            sb.Append(":" + p.Value.Value.ToString());
            if (count < Properties.Count)
            {
                sb.Append(",");
            }
            count++;
        }
        sb.Append("}");
        return sb.ToString();
    }
}

}

它工作得很好,但如果我想使用 JSON 数组,我可能会把自己设计成一个角落。我可以得到一个看起来像这样的字符串:

{"SSN":"300391-1453","LoanAmount":50000.0,"LoanDuration":"2015/02/02","CreditScore":550,"Receipients":[{"bankXML":"bankXML","bankJSON":"bankJSON","bankWeb":"bankWeb"]}

而且我对如何正确取出该数组以及如何存储它一无所知。我的想法是找到数组,然后将数组中的每个对象解析为 JsonProperty,然后将所有属性存储在具有名称值的 JsonArray 中。

我将如何实现这一目标?

【问题讨论】:

  • 您是否有理由不使用 Javascript 序列化程序在 .NET 对象和 JSON 之间移动?
  • @Hopdizzle 如果我想从字符串中添加/删除属性,我觉得拥有具体的 Json 对象更容易管理。我也无法控制序列化程序的输出,这导致了一些问题。
  • @Vipar 现有的序列化程序也允许您反序列化为 Json 对象
  • @SimonBelanger 有什么建议吗?
  • @Vipar 如果你使用 Newtonsoft 库,你可以使用JObject JObject.Parse(string json) 方法

标签: c# json


【解决方案1】:

您从json.org 开始,它具有(除其他外)JSON 语法。然后你编写一个 tokenizer 来读取字符流并发出 tokens 流(例如,空格、数字、字符串、冒号、逗号、方括号(左/右)、花括号(左/右)等。然后您编写一个 解析器 来识别所需的语法并将各种构造转换为所需的表示形式。

或者您可以使用许多预先编写的 JSON 序列化工具中的任何一种。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2016-01-19
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多