Click here to Skip to main content
14,877,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, I'm a Visual FoxPro developer, migrating to C#. I'm currently developing a credit check app using web references. The response I get is in JSON and I'm trying to convert it to xml. The problem I get is that newton soft doesn't want to convert a JSON string/list/non-object to C#. Please assist, this is urgent.

What I have tried:

I've tried converting with xml node, json convert, deserialization, etc
Posted
Updated 29-Nov-17 23:38pm
v2
Comments
F-ES Sitecore 29-Nov-17 8:12am
   
There is nothing wrong with NewstonSoft, it works fine, so the issue must be with how you are using it, but we can't see the relevant code so can't really help. We'd need to see the code as well as the JSON you're trying to convert.
Shamina Maharaj 30-Nov-17 3:43am
   
THE JSON RESULT:



[
{
"Key": "CreditResult",
"Value": [
[
{
"Key": "ConsumerInfo",
"Value": {
"RecordSeq": "01",
"Part": "001",
"PartSeq": "01",
"ConsumerNo": "943599475",
"Surname": "TEST SURNAME",
"Forename1": "TEST NAME",
"Forename2": "",
"Forename3": "",
"Title": "MS",
"Gender": "F",
"NameInfoDate": "20170129",
"DateOfBirth": "20170101",
"IdentityNo1": "1234567891234",
"IdentityNo2": "",
"MaritalStatusCode": "",
"MaritalStatusDesc": "",
"Dependants": "00",
"SpouseName1": "",
"SpouseName2": "",
"TelephoneNumbers": "H(000)0000000 B(0000)0000",
"DeceasedDate": "00000000"
}
},
{
"Key": "LastAddress",
"Value": {
"ConsumerNo": "943599475",
"InformationDate": "20170911",
"Line1": "ABC",
"Line2": "",
"Suburb": "ABC",
"City": "ABC",
"PostalCode": "0000",
"ProvinceCode": "A",
"Province": "ABC",
"AddressPeriod": "00",
"OwnerTenant": "",
"AddressChanged": "Y"
}
}
],
[
{
"Key": "CreditScore",
"Value": {
"ConsumerNo": "943599475",
"PolicyFilters": [
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
],
"Indicators": [
{
"Type": "00",
"Score": "000"
},
{
"Type": "00",
"Score": "0"
},
{
"Type": "00",
"Score": "0"
},
{
"Type": "00",
"Score": "0"
},
{
"Type": "00",
"Score": "0"
},
{
"Type": "00",
"Score": "0"
},
{
"Type": "00",
"Score": "0"
}
],
"ScoreReasons": [
"00",
"00",
"00",
"00",
"00"
],
"RiskBand": "0",
"ReferRiskBand": "0",
"Recommended": "0",
"RuleDescription": ""
}
}
],
[
{
"Key": "FraudPermission",
"Value": true
}
],
[
{
"Key": "Date Requested",
"Value": "2017/11/27 02:06:43 PM"
}
],
[
{
"Key": "User Email",
"Value": "Shamina@oficinagroup.com"
}
],
[
{
"Key": "AuditLog",
"Value": "116315"
}
]
]
}
]

THE CODE ON C# TO REQUEST INFORMATION:

public string doQuickcreditAndFraud(bool doFraud, bool doCredit, string username, string password, string contactFirstName, string contactLastName, string contactID, string clientNumber, string clientEmail, string apiKey)
{
string Item = null;
try
{
Item = this.Proxy.doQuickCreditAndFraud(doFraud, doCredit, username, password, contactFirstName, contactLastName, contactID, clientNumber, clientEmail, apiKey);
}
catch (Exception ex)
{
this.ErrorMessage = ex.Message;
}
var table = JsonConvert.DeserializeObject(Item);
return Item.ToString();

}

I then store

1 solution

That's pretty complex JSON to interpret, and not particularly well designed in my opinion. I think it'll be hard to get something to convert that to XML automatically in a format that is going to look decent, you might need to do some of your own parsing of the data. This might get you started anyway, I haven't implemented all properties or objects, just some basic skeleton details so you'd need to finish it off.

Some classes

[Serializable]
public class CreditResult
{
    [XmlIgnore]
    public string Key { get; set; }

    [XmlIgnore]
    public List<JArray> Value { get; set; }

    [JsonIgnore]
    public ConsumerInfo ConsumerInfo { get; set; }
    [JsonIgnore]
    public LastAddress LastAddress { get; set; }
    [JsonIgnore]
    public CreditScore CreditScore { get; set; }
}

[Serializable]
public class ConsumerInfo 
{
    public string RecordSeq { get; set; }
    public string Part { get; set; }
}

[Serializable]
public class LastAddress
{
    public string ConsumerNo { get; set; }
    public string InformationDate { get; set; }
}

[Serializable]
public class CreditScore
{
    public string ConsumerNo { get; set; }
    public List<string> PolicyFilters { get; set; }
    public List<Indicator> Indicators { get; set; }
}

[Serializable]
public class Indicator
{
    public string Type { get; set; }
    public string Score { get; set; }
}



CreditResult result = JsonConvert.DeserializeObject<List<CreditResult>>(Item).FirstOrDefault();

foreach (JArray subArray in result.Value)
{
    foreach (JObject obj in subArray)
    {
        string objType = obj.GetValue("Key").Value<string>(); // Consumer Info, Last Address etc
                    
        switch (objType)
        {
            case "ConsumerInfo":
                ConsumerInfo consumerInfo = obj.GetValue("Value").ToObject<ConsumerInfo>();
                result.ConsumerInfo = consumerInfo;
                break;
            case "LastAddress":
                LastAddress lastAddress = obj.GetValue("Value").ToObject<LastAddress>();
                result.LastAddress = lastAddress;
                break;
            case "CreditScore":
                CreditScore creditScore = obj.GetValue("Value").ToObject<CreditScore>();
                result.CreditScore = creditScore;
                break;
        }
    }
}

XmlSerializer s = new XmlSerializer(typeof(CreditResult));
MemoryStream memStream = new MemoryStream();
s.Serialize(memStream, result);
memStream.Position = 0;

string xml = new StreamReader(memStream).ReadToEnd();
   

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