-1

I have a JSON that I need to parse and I am parsing it as follows:

JObject json = JObject.Parse(jsonProfile);

The JSON is of the format:

[{ "Id": "mahesh", "GeneralInfo": { "FirstName": "sharma", "LastName": "kanth", "PreferredFirstName": "Akash", "Title": "Designer", "InformalTitle": "Designer", "Gender": "", "Discipline": "", "Department": "Strategy", "BusinessUnit": "", "BrandFunction": "", "ParentAgency": "Public", "Agency": "fish", "AgencyImagePath": "", "Hub": "Public", "SubRegion": "America", "Region": "Americas", "Continent": "North America", "Country": "United States", "State": "Oregon", "Location": "Portland", "Email": "sharma@gmail.com", "SkypeName": "", "Phone": "" }}]

When I run this, I am getting the following error:

{"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."} 

How do I handle the "[" brackets?

Harry
  • 55
  • 2
  • 10
  • 1
    Can you add your json, the complete error and a [mcve]? – aloisdg Oct 04 '18 at 12:02
  • You probably need to parse to `JArray` instead of `JObject`. It would help if we could see the json string or a representative sample of it. – Crowcoder Oct 04 '18 at 12:05
  • We need to see a [mcve] but probably your outermost JSON container is an array not an object. If you don't know in advance what kind of JSON you will be receiving, use `JToken.Parse()` not `JObject.Parse()`. Related: [JSON.NET: Why Use JToken--ever?](https://stackoverflow.com/q/38211719/3744182). – dbc Oct 04 '18 at 12:15
  • updated the question with json sample – Harry Oct 04 '18 at 12:21
  • @Harry your json is not valid. There is a missing `}` at the end – aloisdg Oct 04 '18 at 12:32
  • 1
    "...Current JsonReader item is not an object...". Have you tried `JToken` or `JArray`? You don't have to deal with the "[" brackets directly, you just can't parse to `JObject`. – Crowcoder Oct 04 '18 at 12:32

1 Answers1

0

If you know the json, you can parse your json directly with DeserializeObject:

Try it online!

var root = JsonConvert.DeserializeObject<List<RootObject>>(json);
Console.WriteLine(root[0].GeneralInfo.Email);

output

sharma@gmail.com

class (generated with json2csharp but vs can do it too)

public class GeneralInfo
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PreferredFirstName { get; set; }
    public string Title { get; set; }
    public string InformalTitle { get; set; }
    public string Gender { get; set; }
    public string Discipline { get; set; }
    public string Department { get; set; }
    public string BusinessUnit { get; set; }
    public string BrandFunction { get; set; }
    public string ParentAgency { get; set; }
    public string Agency { get; set; }
    public string AgencyImagePath { get; set; }
    public string Hub { get; set; }
    public string SubRegion { get; set; }
    public string Region { get; set; }
    public string Continent { get; set; }
    public string Country { get; set; }
    public string State { get; set; }
    public string Location { get; set; }
    public string Email { get; set; }
    public string SkypeName { get; set; }
    public string Phone { get; set; }
}

public class RootObject
{
    public string Id { get; set; }
    public GeneralInfo GeneralInfo { get; set; }
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105