将CSV字符串反序列化为C#对象 [英] Deserialize CSV string to an C# Object

查看:122
本文介绍了将CSV字符串反序列化为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了Jira API的回复,要求将其反序列化为数据模型:

I have a response from Jira API, require to be deserialized into data model:

com.atlassian.greenhopper.service.sprint.Sprint@40675167[id=10151,rapidViewId=171,state=CLOSED,name=Sprint 37.1,startDate=2015-07-30T16:00:22.000+03:00,endDate=2015-08-13T16:00:00.000+03:00,completeDate=2015-08-13T14:31:34.343+03:00,sequence=10151]

这实际上是当前有问题的sprint的信息. 我需要将其反序列化为以下模型:

This is actually the information of current sprint for issue. I need to deserialize it to a model like:

public class Model
{
    public string name { get; set; }
    ...
}

我已经使用Regex pattern \[(.*?)\]删除了所有不需要的信息,例如com.atlassian.greenhopper.service.sprint.Sprint@40675167,所以我在里面有方括号.

I have already removed all non-required information, like com.atlassian.greenhopper.service.sprint.Sprint@40675167 using Regex pattern \[(.*?)\] so I have brackets and all inside.

现在,我不再完全尝试寻找一种将该字符串转换为数据模型的方法.

Now I stopped completely trying to find the a way to convert this string to a data model.

推荐答案

找到了以下线程,该内部Object似乎没有JSON表示形式.如该线程示例中所示:

Found the following thread at the Atlassian Answers page and there appears to be no JSON representation of that inner Object. As shown in the example from that thread:

customfield_10007:[
"com.atlassian.greenhopper.service.sprint.Sprint@a29f07[rapidViewId=<null>,state=CLOSED,name=NORD - Sprint 42,startDate=2013-07-29T06:47:00.000+02:00,endDate=2013-08-11T20:47:00.000+02:00,completeDate=2013-08-14T15:31:33.157+02:00,id=107]",
"com.atlassian.greenhopper.service.sprint.Sprint@769133[rapidViewId=<null>,state=ACTIVE,name=NORD - Sprint 43,startDate=2013-08-14T15:32:47.322+02:00,endDate=2013-08-23T15:32:47.322+02:00,completeDate=<null>,id=117]"
],

响应确实是一个JSON数组,但是该数组本身包含CSV数组,因此您可以利用以下内容来解析该内容:

The response is indeed a JSON array, but the array itself contains CSV's, so you can make use of the following to parse that:

   public class DataObject
    {
        public string id { get; set; }
        public string rapidViewId { get; set; }
        public string state { get; set; } 
        public string name { get; set; }
        public string startDate { get; set; }
        public string endDate { get; set; }
        public string completeDate { get; set; }
        public string sequence { get; set; }
    }


    public class Program
    {
        private const string sampleStringData =
            @"[id=10151,rapidViewId=171,state=CLOSED,name=Sprint 37.1,startDate=2015-07-30T16:00:22.000+03:00,endDate=2015-08-13T16:00:00.000+03:00,completeDate=2015-08-13T14:31:34.343+03:00,sequence=10151]";


        static void Main(string[] args)
        {
            var dataObject = new DataObject();

            string[][] splitted;
            var sampleWithNoBrackets = sampleStringData.Substring(1,sampleStringData.Length-2);
            splitted = sampleWithNoBrackets.Split(',').Select(p => p.Split('=')).ToArray();

            dataObject.id           = splitted[0][1];
            dataObject.rapidViewId  = splitted[1][1];
            dataObject.state        = splitted[2][1];
            dataObject.name         = splitted[3][1];
            dataObject.startDate    = splitted[4][1];
            dataObject.endDate      = splitted[5][1];
            dataObject.completeDate = splitted[6][1];
            dataObject.sequence     = splitted[7][1];

            Console.ReadKey();
        }
    }

以下是上面的输出:

这篇关于将CSV字符串反序列化为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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