Deserialize CSV string to an C# Object

爱⌒轻易说出口 提交于 2021-02-10 13:16:36

问题


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]

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; }
    ...
}

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.


回答1:


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]"
],

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

Here's the output for the above:



来源:https://stackoverflow.com/questions/32524313/deserialize-csv-string-to-an-c-sharp-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!