Share via

Cosmos DB | Error reading string

Anonymous
2021-12-18T02:08:08.863+00:00

Hi,

I am trying my first Cosmos DB and trying to read the country items but getting this error:

Error reading string. Unexpected token: StartObject. Path 'country_name', line 1, position 73.

Here is my class:

public List<CountryData> Items { get; private set; }

public class CountryData
{
    public string country_code { get; set; }
    public string country_name { get; set; }
    public string region_name { get; set; }

    public string country_flag
    {
        get
        {
            return string.Format("https://www.mydomain.com/flags/square/{0}.png", country_code);
        }
    }
}

and this is my data:

{
    "country_code": "US",
    "country_name": {
        "en": "United States"
    },
    "id": "e457cd45-aa45-4579-8a82-f8e612663cad",
    "_rid": "o891AJJ-reoEAAAAAAAAAA==",
    "_self": "dbs/o891AA==/colls/o891AJJ-reo=/docs/o891AJJ-reoEAAAAAAAAAA==/",
    "_etag": "\"00002113-0000-3200-0000-61b67f2c0000\"",
    "_attachments": "attachments/",
    "_ts": 1639350060
}

I know it might be the problem with the "en" in the items but how can I solve it?

Thanks,
Jassim

Developer technologies | .NET | Xamarin
Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

0 comments No comments

Answer accepted by question author

Alon Fliess 261 Reputation points Microsoft Regional Director
2021-12-18T03:39:24.777+00:00

The class CountryData is incorrect, you must have a type entry for the property country_name. I just paste your JSON as classes with the special paste of VisualStudio and got this:

public class Rootobject
{
    public string country_code { get; set; }
    public Country_Name country_name { get; set; }
    public string id { get; set; }
    public string _rid { get; set; }
    public string _self { get; set; }
    public string _etag { get; set; }
    public string _attachments { get; set; }
    public int _ts { get; set; }
}

public class Country_Name
{
    public string en { get; set; }
}

Alon.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.