c# - 使用数组读取 JSON 文件

标签 c# json

我目前正在使用 C# 进行 JSON 字符串提取。我的 JSON 字符串由具有重复键的数组组成。不确定我的描述是否正确,因为我是新手。

这是我的 JSON 字符串

{"Index":
{   "LibraryName":"JGKing"
    ,   "FormName":"AccountsPayable"
    ,   "User":null
    ,   "FilingPriority":null
    ,   "FileDescription":null
    ,   "Fields":
            {"Field":
                [
                    {       "Name":"invItemID"
                        ,   "Value":"6276"
                    }
                    ,{      "Name":"invEntityCode"
                    ,       "Value":"16"
                    }

                    ,{      "Name":"invVendorCode"
                    ,       "Value":"MIRUB01"
                    }

                    ,{      "Name":"invNumber"
                    ,       "Value":"PWD5"
                    }

                    ,{      "Name":"invDate"
                    ,       "Value":"2017-08-21"
                    }

                    ,{      "Name":"invStatus"
                    ,       "Value":""
                    }

                    ,{      "Name":"invCurrencyCode"
                    ,       "Value":"AU"
                    }

                    ,{      "Name":"invCurrencyRate"
                    ,       "Value":"1"
                    }

                    ,{      "Name":"invTax"
                    ,       "Value":"454.3"
                    }

                    ,       {"Name":"invTotal"
                    ,       "Value":"4997.27"
                    }

                    ,       {"Name":"invReceivedDate"
                    ,       "Value":"2017-09-06"
                    }

                    ,{      "Name":"InvoiceLine1"
                    ,       "Value":"{\r\n  \"invLineNumber\": \"1\",\r\n  \"invPONumber\": \"\",\r\n  \"invPOLineNo\": \"1\",\r\n  \"invPOJobID\": \"\",\r\n  \"invCostCode\": \"\",\r\n  \"invCategory\": \"\",\r\n  \"invGLCode\": \"61-01-49-6862.517\",\r\n  \"invDescription\": \"\",\r\n  \"invEntryType\": \"\",\r\n  \"invAmount\": \"4542.97\",\r\n  \"invTaxAmount\": \"454.3\",\r\n  \"invTaxCode\": \"GST\",\r\n  \"invAmountIncTax\": \"4997.27\"\r\n}"}]}}}

我需要提取数组内的 invItemID 键的值。 我尝试从类中序列化我的 json 字符串,但它在 List<>

中返回 null

这是我的代码

    public void CFExport(string jsonFile)
    {
        string ItemIDField;
        string ItemIDValue;

            using (StreamReader r = new StreamReader(jsonFile))
            {
                JsonSerializer s = new JsonSerializer();
                var Idx = (JSONMain)s.Deserialize(r, typeof(JSONMain));
                var flds = (Fields)s.Deserialize(r, typeof(Fields));

                if (flds != null)
                {
                    foreach (var _field in flds.Field)
                    {
                        ItemIDField = _field.Name;
                        ItemIDValue = _field.Value;
                    }
                }
            }
    }


public class JSONMain
{
    public Index Index { get; set; }
}

public class Index
{
    public string LibraryName { get; set; }
    public string FormName { get; set; }
    public string User { get; set; }
    public string FilingPriority { get; set; }
    public string FileDescription { get; set; }

}

public class Fields
{
    public List<Field> Field { get; set; }
}

public class Field
{ 
    public string Name { get; set; }
    public string Value { get; set; }

}

希望你能帮助我。

提前致谢

最佳答案

尝试用您的类反射(reflect) JSON 文件,如下所示:

public class Index
{
    public string LibraryName { get; set; }
    public string FormName { get; set; }
    public string User { get; set; }
    public string FilingPriority { get; set; }
    public string FileDescription { get; set; }
    public Fields Fields { get; set; } //this line makes the difference
}

如果您现在反序列化,这些字段应该会自动填充。我还建议您使用 JsonConvert.Deserialze<>()因为它更容易一些(请参阅 documentation )并且您是这个主题的新手。

获取 invItemID 的值可能看起来像这样:

public void CFExport(string jsonFile)
{
    string ItemIDField = "invItemID";
    string ItemIDValue;

    using (StreamReader r = new StreamReader(jsonFile))
    {
        var Idx = JsonConvert.DeserializeObject<JSONMain>(r);

        foreach(var field in Idx.Index.Fields.Field) 
        {
            if(field.Name == ItemIDField)
            {
                ItemIDValue = field.Value;
            }
        }
    }
}

呼呼。我在 Stackoverflow 上的第一个回答!我希望这对您有帮助。

关于c# - 使用数组读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52961687/

相关文章:

c# - 用填充索引替换字符

c# - C# 中需要可选的 VB 参数

c# - 使用 iTextSharp 库提取 pdf 文件中包含的签名图像

javascript - Backbone.js 添加到集合

java - 将字符串数组转换为 JSON 对象?

java - 使用 FlexJSON 将 JSON 解析为 Map<String, Entity>

javascript - json stringify 不能在 asp.net mvc 页面上工作

mysql - 为什么变量没有被传递?

javascript - JSON.Parse,'未捕获的语法错误 : Unexpected token o

c# - VS2012 中的 Windows Phone 8 SDK,图标?