在C#中将JSON非对象转换为XML [英] Converting JSON non-object to XML in C#

查看:88
本文介绍了在C#中将JSON非对象转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是一名Visual FoxPro开发人员,正在迁移到C#。我目前正在使用网络参考开发信用检查应用程序。我得到的响应是JSON,我试图将其转换为xml。我得到的问题是newton soft不想将JSON字符串/列表/非对象转换为C#。请协助,<罢工>这是紧急的。



我尝试过:



我试过用xml节点转换,json转换,反序列化等等

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

推荐答案

这是一个相当复杂的JSON来解释,而不是特别的在我看来设计得很好。我认为要以一种看起来不错的格式自动将其转换为XML是很困难的,您可能需要对数据进行一些自己的解析。无论如何,这可能会让你开始,我没有实现所有的属性或对象,只是一些基本的骨架细节,所以你需要完成它。



有些课程



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();


这篇关于在C#中将JSON非对象转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆