Click here to Skip to main content
15,887,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a JSON string that I am trying to convert. I can get the main part but there are two elements which are arrays part of the main that are not populating when I run the Deserializtion.

Below is the data and the code
{"Links":[{"rel":"self","href":"company"}],"License":{"Resources":[{"Access":"Full","Name":"account"},{"Access":"Full","Name":"amenity"},{"Access":"Full","Name":"aponlinecontrol"},{"Access":"Full","Name":"aponlineuser"},{"Access":"Full","Name":"balance"},{"Access":"Full","Name":"bank"},{"Access":"Full","Name":"board"},{"Access":"Full","Name":"cashapplicationorder"},{"Access":"Full","Name":"charge"},{"Access":"Full","Name":"chargebatch"},{"Access":"Full","Name":"chargecode"},{"Access":"Full","Name":"chargeitem"},{"Access":"Full","Name":"check"},{"Access":"Full","Name":"communication"},{"Access":"Full","Name":"communicationbatch"},{"Access":"Full","Name":"community"},{"Access":"Full","Name":"company"},{"Access":"Full","Name":"contractor"},{"Access":"Full","Name":"email"},{"Access":"Full","Name":"emailqueue"},{"Access":"Full","Name":"factsheettype"},{"Access":"Full","Name":"flexvalue"},{"Access":"Full","Name":"general"},{"Access":"Full","Name":"insurance"},{"Access":"Full","Name":"invoice"},{"Access":"Full","Name":"journalentry"},{"Access":"Full","Name":"legal"},{"Access":"Full","Name":"messenger"},{"Access":"Full","Name":"note"},{"Access":"Full","Name":"officer"},{"Access":"Full","Name":"owner"},{"Access":"Full","Name":"partnership"},{"Access":"Full","Name":"property"},{"Access":"Full","Name":"receiptbatch"},{"Access":"Full","Name":"receiptitem"},{"Access":"Full","Name":"relationship"},{"Access":"Full","Name":"specialinfo"},{"Access":"Full","Name":"topsuser"},{"Access":"Full","Name":"vendor"},{"Access":"Full","Name":"ccr"},{"Access":"Full","Name":"ccraction"},{"Access":"Full","Name":"ccrcode"},{"Access":"Full","Name":"ccrstage"},{"Access":"Full","Name":"relationship"},{"Access":"Full","Name":"vehicle"},{"Access":"Full","Name":"visitor"},{"Access":"Full","Name":"poolpass"},{"Access":"Full","Name":"servicerequest"},{"Access":"Full","Name":"workorder"},{"Access":"Full","Name":"workorderauthorization"},{"Access":"Full","Name":"workordertype"}],"CommunityApiKey":[{"Active":true,"ApiKey":"EED88E61-1AB6-4184-B249-B5D2ADC1F1F3","CommunityID":"230","CommunityKey":16,"CommunityName":"Pointe South Mountain","CustomerName":"Client DAB"},{"Active":true,"ApiKey":"CC454116-8563-4CA9-A6EE-0DADE2CBE7BE","CommunityID":"SBY","CommunityKey":59,"CommunityName":"Southbay Yacht & Racquet Club","CustomerName":"Client DAB"},{"Active":true,"ApiKey":"6803BDDD-8B1F-48E5-B75D-79D8896D4903","CommunityID":"XX","CommunityKey":72,"CommunityName":"Sample Condominium","CustomerName":"Client DAB"}],"PartnerServices":[]},"Address1":"PO BOX 12532","Address2":"","City":"Tempe","Contact":"David Black","Email1":"","Email2":"","Fax1":"","Fax2":"","Name":"Black and Associates LLC","Phone1":"602-509-6217","Phone2":"","State":"AZ","UtcOffset":null,"Zip":"85284","Metadata":{}}


CODE;
VB.NET
Public json As String

'Company
Public Class License
    Public Property Resources As List(Of Resrcs)
    Public Property CommunityApiKey As List(Of CommApiKeys)
    Public Property Address1 As String
    Public Property Address2 As String
    Public Property City As String
    Public Property Contact As String
    Public Property Email1 As String
    Public Property Email2 As String
    Public Property Fax1 As String
    Public Property Fax2 As String
    Public Property Name As String
    Public Property Phone1 As String
    Public Property Phone2 As String
    Public Property State As String
    Public Property UtcOffset As Nullable(Of Long)
    Public Property Zip As String
End Class
' Resources
Public Class Resrcs
    Public Property Access() As String
    Public Property Name() As String
End Class
' Community API Keys
Public Class CommApiKeys
    Public Property Active() As Boolean
    Public Property ApiKey() As String
    Public Property CommunityID() As String
    Public Property CommunityKey() As Long
    Public Property CommunityName() As String
    Public Property CustomerName() As String
End Class

    Dim company = Newtonsoft.Json.JsonConvert.DeserializeObject(Of License)(json)


What I have tried:

I have tried a couple different forms of deserialization but none of them seem to work. I have other JSON data with similar format and they work ok.
Posted
Comments
Garth J Lancaster 16-Feb-16 19:57pm    
how did you generate your VB.Net classes (License, Resrcs, CommApiKeys) ? I copied your json into JSON2CSHARP and received more classes than you have (I always use json2csharp as a guide, then 'optimise' later) ... can this (below) be translated to VB.Net perhaps and tried ?


public class Link
{
public string rel { get; set; }
public string href { get; set; }
}

public class Resource
{
public string Access { get; set; }
public string Name { get; set; }
}

public class CommunityApiKey
{
public bool Active { get; set; }
public string ApiKey { get; set; }
public string CommunityID { get; set; }
public int CommunityKey { get; set; }
public string CommunityName { get; set; }
public string CustomerName { get; set; }
}

public class License
{
public List<resource> Resources { get; set; }
public List<communityapikey> CommunityApiKey { get; set; }
public List<object> PartnerServices { get; set; }
}

public class Metadata
{
}

public class RootObject
{
public List<Link> Links { get; set; }
public License License { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Contact { get; set; }
public string Email1 { get; set; }
public string Email2 { get; set; }
public string Fax1 { get; set; }
public string Fax2 { get; set; }
public string Name { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string State { get; set; }
public object UtcOffset { get; set; }
public string Zip { get; set; }
public Metadata Metadata { get; set; }
}
Dave-10169531 17-Feb-16 12:26pm    
thank you the JSON2CSHARP helped me to see how my class was not defined properly it is now working.

I will use the JSON2CSHARP in the future.
Sergey Alexandrovich Kryukov 16-Feb-16 20:36pm    
The question is: what creates JSON string? If this is .NET code, too, you should better use DataContractJsonSerializer. If not, you should better model your class after already existing JSON. Or you can parse it into object using System.Web.Script.Serialization.JavaScriptSerializer and map it to yours.
—SA

1 solution

Please see my comment to the question. Your choice depends on your scenario. For further detail, please see my past answers:
How To Convert object type to C# class object type[^],
how to conver multi level json data to C# Object?[^],
haw to get data from Cloudant (json document)[^],
deseralize a json string array[^].

—SA
 
Share this answer
 

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