Click here to Skip to main content
14,929,005 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am calling a REST API and receives a response as JSON text with a format that JSON Editor https://jsoneditoronline.org/ cannot read either.

Part of the JSON text looks like this:

"{\"displayFieldName\":\"address\",\"fieldAliases\":{\"objectid\":\"objectid\",\"address_id\":\"adressutal\",\"address\":\"adressa\",\"house_no\":\"húsanr.\",\"house_digit\":\"húsatal\",\"house_letter\":\"húsastavur.\",\"street_id\":\"vegur ID\",\"street_name\":\"veganavn.\",\"zip\":\"postnr.\",\"city\":\"stað\",\"municipality\":\"kommuna\",\"cadastral_district\":\"markatalsbygd\",\"cadastral_no\":\"matr. nr.\",\"longitude\":\"longdarstig\",\"latitude\":\"breiddarsting\",\"google_streets_url\":\"vís á google streets\",\"address_city\":\"adressa og bygd\",\"cadastral_mslink\":\"mslink av matrikul\"},\"geometryType\":\"esriGeometryPoint\",\"spatialReference\":{\"wkid\":5316,\"latestWkid\":5316},\"fields\":[{\"name\":\"objectid\",\"type\":\"esriFieldTypeOID\",\"alias\":\"objectid\"},{\"name\":\"address_id\",\"type\":\"esriFieldTypeInteger\",\"alias\":\"adressutal\"},{\"name\":\"address\",\"type\":\"esriFieldTypeString\",\"alias\":\"adressa\",\"length\":60},{\"name\":\"house_no\",\"type\":\"esriFieldTypeString\",


The text contains a list of addresses with the following structure:

public class Point
{
public int X { get; set; }
public int Y { get; set; }
}

public class Address
{
public int objectid { get; set; }
public int address_id { get; set; }
public string address { get; set; }
public int house_no { get; set; }
public int house_digit { get; set; }
public int house_letter { get; set; }
public int street_id { get; set; }
public string street_name { get; set; }
public int zip { get; set; }
public string city { get; set; }
public int municipality { get; set; }
public int cadastral_district { get; set; }
public int cadastral_no { get; set; }
public int longitude { get; set; }
public int latitude { get; set; }
public string google_streets_url { get; set; }
public string address_city { get; set; }
public string cadastral_mslink { get; set; }
public Point Point { get; set; }
}

How can I deserialize such a JSON text?

What I have tried:

I have tried this code:

var objectList = JsonConvert.DeserializeObject
(jsonResponse);

but it does not work.
Posted
Updated 6-Jun-19 1:15am

If that is your complete JSON string, then no, it's not going to work - it's incomplete and missing a few close brackets, both square and curly. Do remember that when you view a string in Visual Studio it shows double quotes as escaped characters: '"' will be shown as '\"' as in your text - if the escape character is physically there in your string, then JSON will not process it correctly.

Adding the closers and removing the escapes gives this:
{"displayFieldName":"address","fieldAliases":{"objectid":"objectid","address_id":"adressutal","address":"adressa","house_no":"húsanr.","house_digit":"húsatal","house_letter":"húsastavur.","street_id":"vegur ID","street_name":"veganavn.","zip":"postnr.","city":"stað","municipality":"kommuna","cadastral_district":"markatalsbygd","cadastral_no":"matr. nr.","longitude":"longdarstig","latitude":"breiddarsting","google_streets_url":"vís á google streets","address_city":"adressa og bygd","cadastral_mslink":"mslink av matrikul"}
,"geometryType":"esriGeometryPoint","spatialReference":{"wkid":5316,"latestWkid":5316}
,"fields":[{"name":"objectid","type":"esriFieldTypeOID","alias":"objectid"}
,{"name":"address_id","type":"esriFieldTypeInteger","alias":"adressutal"}
,{"name":"address","type":"esriFieldTypeString","alias":"adressa","length":60}
,{"name":"house_no","type":"esriFieldTypeString"} ]}
Which does parse according to json2csharp - generate c# classes from json[^] and it suggests the classes:
public class FieldAliases
{
    public string objectid { get; set; }
    public string address_id { get; set; }
    public string address { get; set; }
    public string house_no { get; set; }
    public string house_digit { get; set; }
    public string house_letter { get; set; }
    public string street_id { get; set; }
    public string street_name { get; set; }
    public string zip { get; set; }
    public string city { get; set; }
    public string municipality { get; set; }
    public string cadastral_district { get; set; }
    public string cadastral_no { get; set; }
    public string longitude { get; set; }
    public string latitude { get; set; }
    public string google_streets_url { get; set; }
    public string address_city { get; set; }
    public string cadastral_mslink { get; set; }
}

public class SpatialReference
{
    public int wkid { get; set; }
    public int latestWkid { get; set; }
}

public class Field
{
    public string name { get; set; }
    public string type { get; set; }
    public string alias { get; set; }
    public int? length { get; set; }
}

public class RootObject
{
    public string displayFieldName { get; set; }
    public FieldAliases fieldAliases { get; set; }
    public string geometryType { get; set; }
    public SpatialReference spatialReference { get; set; }
    public List<Field> fields { get; set; }
}
Which does not match your classes!
   
A good tool to generate your C# classes from JSON is JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^] . With regards to deserialization, I have written an article that will answer all your questions: Working with JSON in C# & VB[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900