简体   繁体   中英

Deserialization with json.net

I am coming across a very weird situation. I have two sections of code where two separate rest calls are made to web service and the json from the web server is deserialized using Json.Net. Here are my sections of code

1st Section:

string AuthoraizationTicket = "";
                using (var client = new WebClient())
                {
                    string url = "https://maxcvservices.dnb.com/Authentication/V2.0/";

                    client.Headers.Add("x-dnb-user", "P200000216EA4E620E64DD980D494E7A");
                    client.Headers.Add("x-dnb-pwd", "password#1");

                    try
                    {
                        var response = client.DownloadString(url);
                        var DeserializedJson = JsonConvert.DeserializeObject<RootObject>(response);
                        AuthoraizationTicket = DeserializedJson.AuthenticationDetail.Token;

                    }
                    catch (Exception ex)
                    {

                    }

The above section executes correctly and Json is Deserialized Properly.

Section 2:

using (var wClient = new WebClient())
                    {
                        string DUNS = txtDUNS.Value;
                        string RTNG_TRND = String.Format("https://maxcvservices.dnb.com/V5.0/organizations/{0}/products/RTNG_TRND", DUNS);
                        wClient.Headers.Add("Authorization", AuthoraizationTicket);

                        try
                        {
                            var resp = wClient.DownloadString(RTNG_TRND);
                            var RatingAndTrend = JsonConvert.DeserializeObject<RatingAndTrendDnB>(resp);
                            string creditCondition = RatingAndTrend.OrderProductResponse.OrderProductResponseDetail.Product.Organization.Assessment.FinancialConditionText.Value;
                            }
                        catch (Exception ex)
                        {

                        }
                    }

The above section throws an exception at

var RatingAndTrend = JsonConvert.DeserializeObject<RatingAndTrendDnB>(resp);

Line saying:

Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

Here are my classes for Deserialization

public class TransactionDetail
{
    public string ServiceTransactionID { get; set; }
    public string TransactionTimestamp { get; set; }
    public string ApplicationTransactionID { get; set; }
}

public class ResultMessage
{
    public string ResultDescription { get; set; }
}

public class TransactionResult
{
    public string SeverityText { get; set; }
    public string ResultID { get; set; }
    public string ResultText { get; set; }
    public ResultMessage ResultMessage { get; set; }
}

public class AuthenticationDetail
{
    public string Token { get; set; }
}

public class RootObject
{
    public TransactionDetail TransactionDetail { get; set; }
    public TransactionResult TransactionResult { get; set; }
    public AuthenticationDetail AuthenticationDetail { get; set; }
}

public class InquiryDetail
{
    public string DUNSNumber { get; set; }
    public string CountryISOAlpha2Code { get; set; }
}

public class SubjectHeader
{
    public string DUNSNumber { get; set; }
}

public class TelephoneNumber
{
    public string TelecommunicationNumber { get; set; }
    public string InternationalDialingCode { get; set; }
}

public class Telecommunication
{
    public List<TelephoneNumber> TelephoneNumber { get; set; }
}

public class StreetAddressLine
{
    public string LineText { get; set; }
}

public class PrimaryAddress
{
    public List<StreetAddressLine> StreetAddressLine { get; set; }
    public string PrimaryTownName { get; set; }
    public string CountryISOAlpha2Code { get; set; }
    public string TerritoryAbbreviatedName { get; set; }
    public string PostalCode { get; set; }
    public string TerritoryOfficialName { get; set; }
}

public class Location
{
    public List<PrimaryAddress> PrimaryAddress { get; set; }
}

public class OrganizationName2
{
    [JsonProperty("$")]
    public string Value { get; set; }
}

public class OrganizationPrimaryName
{
    public OrganizationName2 OrganizationName { get; set; }
}

public class OrganizationName
{
    public List<OrganizationPrimaryName> OrganizationPrimaryName { get; set; }
}

public class FamilyTreeMemberRoleText
{
    [JsonProperty("DNBCodeValue")]
    public int DNBCodeValue { get; set; }
    [JsonProperty("$")]
    public string Value { get; set; }
}

public class FamilyTreeMemberRole
{
    public FamilyTreeMemberRoleText FamilyTreeMemberRoleText { get; set; }
}

public class OrganizationDetail
{
    public List<FamilyTreeMemberRole> FamilyTreeMemberRole { get; set; }
}

public class DNBStandardRating
{
    [JsonProperty("DNBStandardRating")]
    public string Rating { get; set; }
}

public class HistoryRatingText
{
    [JsonProperty("DNBCodeValue")]
    public int DNBCodeValue { get; set; }
    [JsonProperty("$")]
    public string Value { get; set; }
}

public class FinancialConditionText
{
    [JsonProperty("DNBCodeValue")]
    public int DNBCodeValue { get; set; }
    [JsonProperty("$")]
    public string Value { get; set; }
}

public class Assessment
{
    public DNBStandardRating DNBStandardRating { get; set; }
    public HistoryRatingText HistoryRatingText { get; set; }
    public FinancialConditionText FinancialConditionText { get; set; }
}

public class Organization
{
    public SubjectHeader SubjectHeader { get; set; }
    public Telecommunication Telecommunication { get; set; }
    public Location Location { get; set; }
    public OrganizationName OrganizationName { get; set; }
    public OrganizationDetail OrganizationDetail { get; set; }
    public Assessment Assessment { get; set; }
}

public class ArchiveDetail
{
    public int PortfolioAssetID { get; set; }
}

public class Product
{
    public string DNBProductID { get; set; }
    public Organization Organization { get; set; }
    public ArchiveDetail ArchiveDetail { get; set; }
}

public class OrderProductResponseDetail
{
    public InquiryDetail InquiryDetail { get; set; }
    public Product Product { get; set; }
}

public class OrderProductResponse
{
    [JsonProperty("@ServiceVersionNumber")]
    public string ServiceVersionNumber { get; set; }
    public TransactionDetail TransactionDetail { get; set; }
    public TransactionResult TransactionResult { get; set; }
    public OrderProductResponseDetail OrderProductResponseDetail { get; set; }
}

public class RatingAndTrendDnB
{
    public OrderProductResponse OrderProductResponse { get; set; }
}

And my Json for section 2:

{"OrderProductResponse":{"@ServiceVersionNumber":"5.0","TransactionDetail":{"ServiceTransactionID":"Id-70e1fecb52ff474c207b0000-2","TransactionTimestamp":"2014-02-15T05:54:10.834-05:00"},"TransactionResult":{"SeverityText":"Information","ResultID":"CM000","ResultText":"Success"},"OrderProductResponseDetail":{"InquiryDetail":{"DUNSNumber":"804735132","CountryISOAlpha2Code":"US"},"Product":{"DNBProductID":"RTNG_TRND","Organization":{"SubjectHeader":{"DUNSNumber":"804735132"},"Telecommunication":{"TelephoneNumber":[{"TelecommunicationNumber":"(650) 555-0000","InternationalDialingCode":"1"}]},"Location":{"PrimaryAddress":[{"StreetAddressLine":[{"LineText":"492 Koller St"}],"PrimaryTownName":"San Francisco","CountryISOAlpha2Code":"US","TerritoryAbbreviatedName":"CA","PostalCode":"94110","TerritoryOfficialName":"California"}]},"OrganizationName":{"OrganizationPrimaryName":[{"OrganizationName":{"$":"Gorman Manufacturing Company, Inc."}}]},"OrganizationDetail":{"FamilyTreeMemberRole":[{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12774,"$":"Domestic Ultimate"}},{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12775,"$":"Global Ultimate"}},{"FamilyTreeMemberRoleText":{"@DNBCodeValue":12773,"$":"Parent"}}]},"Assessment":{"DNBStandardRating":{"DNBStandardRating":"3A4"},"HistoryRatingText":{"@DNBCodeValue":9078,"$":"Clear"},"FinancialConditionText":{"@DNBCodeValue":415,"$":"Fair"}}},"ArchiveDetail":{"PortfolioAssetID":47651715}}}}}

Any help will be appreciated

It appears that section 2 is either:

  • A: Running in a different assembly than section 1, and does not have the Newtonsoft reference.
  • B: Referencing a different version of Newtonsoft.JSON.

Ensure in your NuGet package system (if that is what you are using for references) that you have the same version of Newtonsoft.JSON installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM