1

I cannot read json string with c#. I getting error while reading.

Json file

{
    "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."

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

But everything right, I do not see bugs.

3
  • 4
    you are attempting to deserialize a json string that is not an array to an array object. Commented Jun 27, 2016 at 19:57
  • how can I read this? Commented Jun 27, 2016 at 19:59
  • Create your own class to Deserialize this JSON string. Commented Jun 27, 2016 at 20:02

3 Answers 3

4

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:

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.

var parsedJson = JsonConvert.DeserializeObject<Invoices>(json);
Sign up to request clarification or add additional context in comments.

1 Comment

Internet points for mentioning json2csharp.com - I swear by it, as it's saved my bacon a fair few times :)
2

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

Comments

1

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.

You are trying to parse the object as a JArray while your JSON isn't for an array.

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."

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.

{ is the first character of your JSON, it was an array than it should start with [ just like if you were writing 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.

this should point you to it being a complex object and not an array and David L's answer covers that perfectly.

I just wanted to touch on better understanding the error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.