简体   繁体   English

使用Newtonsoft C#读取Json字符串

[英]Read Json string with Newtonsoft C#

I cannot read json string with c#. 我无法使用C#读取json字符串。 I getting error while reading. 阅读时出现错误。

Json file Json文件

{
    "Invoice": {
        "Description": "New",
        "InvoiceTypeId": "3d166468-3923-11e6-9e7c-40e230cfb8ae",
        "CustomerAccountsId": "TEST",
        "InvoiceDate": "2016-06-27",
        "PayableDate": "2016-06-27",
        "Prefix": "A",
        "Serial": "34222",
        "tag": "TEST"
    },
    "InvoiceLine": [
        {
            "ValueId": "c41d3d85-3a1e-11e6-9e7c-40e230cfb8ae",
            "Qantity": "3",
            "UnitId": "a72e0dde-3953-11e6-9e7c-40e230cfb8ae",
            "Price": "1.500,00",
            "VatRateId": "18",
            "LineVat": "810,00",
            "LineTotal": "5.310,00",
            "Vat": "00a239f1-3c3a-11e6-9e7c-40e230cfb8ae"
        },
        {
            "ValueId": "fd11b236-3952-11e6-9e7c-40e230cfb8ae",
            "Qantity": "5",
            "UnitId": "a72e0dde-3953-11e6-9e7c-40e230cfb8ae",
            "Price": "1.000,00",
            "VatRateId": "18",
            "LineVat": "900,00",
            "LineTotal": "5.900,00",
            "Vat": "00a239f1-3c3a-11e6-9e7c-40e230cfb8ae"
        }
    ]
}

"Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1." “从JsonReader读取JArray时出错。当前JsonReader项不是数组:StartObject。路径,第1行,位置1。”

JArray jsonVal = JArray.Parse(jsonArr) as JArray;
dynamic vars = jsonVal;

But everything right, I do not see bugs. 但是一切正常,我没有看到错误。

Don't do this...you're parsing to an array when it's clearly a complex object. 不要这样做...当它显然是一个复杂的对象时,您正在解析一个数组。

Use a converter such as Visual Studio or json2csharp.com to create the appropriate target object structure: 使用诸如Visual Studio或json2csharp.com之类的转换器来创建适当的目标对象结构:

public class Invoice
{
    public string Description { get; set; }
    public string InvoiceTypeId { get; set; }
    public string CustomerAccountsId { get; set; }
    public string InvoiceDate { get; set; }
    public string PayableDate { get; set; }
    public string Prefix { get; set; }
    public string Serial { get; set; }
    public string tag { get; set; }
}

public class InvoiceLine
{
    public string ValueId { get; set; }
    public string Qantity { get; set; }
    public string UnitId { get; set; }
    public string Price { get; set; }
    public string VatRateId { get; set; }
    public string LineVat { get; set; }
    public string LineTotal { get; set; }
    public string Vat { get; set; }
}

public class Invoices
{
    public Invoice Invoice { get; set; }
    public List<InvoiceLine> InvoiceLine { get; set; }
}

Then simply use the JsonConvert methods to deserialize. 然后,只需使用JsonConvert方法反序列化即可。

var parsedJson = JsonConvert.DeserializeObject<Invoices>(json);

You can also deserialize it to a dynamic object. 您还可以将其反序列化为动态对象。 Of course this is not ideal but if you don't want to create a whole new type for it, this would work 当然这不是理想的选择,但是如果您不想为其创建一个全新的类型,那将是可行的

var vars =JsonConvert.DeserializeObject<dynamic>(  jsonArr);
//vars.InvoiceLine

I noticed in the question and a comment you made, "How do I read this?" 我在问题和您发表的评论中注意到“我怎么读?” that you might also be confused on reading JSON which made the error message confusing to you. 您可能还会在阅读JSON时感到困惑,这使错误消息使您感到困惑。

You are trying to parse the object as a JArray while your JSON isn't for an array. 您正在尝试将对象解析为JArray,而您的JSON不用于数组。

If you look at your error message, 如果您查看错误消息,

"Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1." “从JsonReader读取JArray时出错。当前JsonReader项不是数组:StartObject。路径,第1行,位置1。”

It's immediately throwing an error at the first character on the first line of your JSON and telling you it's not an array. 它立即在JSON第一行的第一个字符处引发错误,并告诉您它不是数组。

{ is the first character of your JSON, it was an array than it should start with [ just like if you were writing JavaScript. {是JSON的第一个字符,它是一个数组,其开头应为[ ,就像编写JavaScript一样。

Array: 数组:

var javaScriptArray = ["This", "Is", "An", "Array"];

Object: 宾语:

var javaScriptObject = { id: 0, thisIsAnArray: false };

And put it together: 并将其放在一起:

var javaScriptArrayOfObjects = [ { id: 0, thisIsAnArray: false},{ id: 1, thisIsAnArray: false} ];

You can see that when you look at your JSON ( Java Script Object Notation ) you should be able to tell if it's an array of objects or an object with arrays. 您可以看到,当您查看JSON( Java脚本对象表示法 )时,您应该能够知道它是对象数组还是带有数组的对象。

this should point you to it being a complex object and not an array and David L's answer covers that perfectly. 这应该指出您它是一个复杂的对象而不是一个数组,而David L的答案完美地涵盖了这一点。

I just wanted to touch on better understanding the error message. 我只是想更好地理解错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM