Vanessa Kensington
Vanessa Kensington

Reputation: 139

serializing long string full of different objects to json

I have text data that I would like to transfer to json. I have decided to split them based on '|' an then serialize. The example below gives the object the same value for each property. I realize that its the iteration however I can't think of a way to do it differently. Could you please share your thoughts?

var test = "Australia | dolar | 1 | AUD | 15,608 | Brasil | real | 1 | BRL 4,016 | Bulgaria | lev | 1 | BGN | 13,066 | China | žen-min-pi | 1 | CNY | 3,369";

         var  split = test.Split('|');
        
         var list = new List<DailyCourse>();
         foreach (var variable in split)
         {
             var model = new DailyCourse()
             {
                 Country = variable,
                 Currency = variable,
                 Amount = variable,
                 Code = variable,
                 Course = variable
             };
         }
         
         list.Add(model);
         var json = JsonSerializer.Serialize(list);
         
         Console.WriteLine(json);

the output now is like

[{"Date":null,"Country":"Austr\u00E1lie ","Currency":"Austr\u00E1lie ","Amount":"Austr\u00E1lie ","Code":"Austr\u00E1lie ","Course":"Austr\u00E1lie "}, ...

Upvotes: 0

Views: 326

Answers (2)

Neil W
Neil W

Reputation: 9237

Your data is in groups of 5, but you are then iterating over each individual element and creating object for each (obviously with the same value in each property).

You need to iterate in steps of 5 and grab the sequence of elements:

var test = "Australia | dolar | 1 | AUD | 15,608 | Brasil | real | 1 | BRL 4,016 | Bulgaria | lev | 1 | BGN | 13,066 | China | žen-min-pi | 1 | CNY | 3,369";

var  split = test.Split('|');

for (var i = 0; i < split.Length; i = i + 5)
{
    var model = new DailyCourse()
    {
        Country = split[i];
        Currency = split[i + 1];
        Amount = split[i + 2];
        Code = split[i + 3];
        Course = split[i + 4];
    }
}
         
list.Add(model);
var json = JsonSerializer.Serialize(list);
         
Console.WriteLine(json);

Upvotes: 4

Serge
Serge

Reputation: 43931

Try this


    var test = "Australia | dolar | 1 | AUD | 15,608 | Brasil | real | 1 | BRL| 4,016 | Bulgaria | lev | 1 | BGN | 13,066 | China | žen-min-pi | 1 | CNY | 3,369";

    var split = test.Split('|');

    var list = new List<DailyCourse>();
    var i=0;
    while ( i < split.Count())
    {
        var model = new DailyCourse();
        
            model.Country = split[i]; i++;
            model.Currency = split[i]; i++;
            model.Amount = Convert.ToInt32( split[i]); i++;
            model.Code = split[i]; i++;
            model.Course = Convert.ToDouble( split[i]); i++;
            
        list.Add(model);
        
    } 
    
    var json = JsonSerializer.Serialize(list);

output

[{"Country":"Australia ","Currency":" dolar ","Amount":1,"Code":" AUD ","Course":15608},
{"Country":" Brasil ","Currency":" real ","Amount":1,"Code":" BRL","Course":4016},
{"Country":" Bulgaria ","Currency":" lev ","Amount":1,"Code":" BGN ","Course":13066},
{"Country":" China ","Currency":" \u017Een-min-pi ","Amount":1,"Code":" CNY ","Course":3369}]

class

public class DailyCourse
{

    public string Country { get; set; }
    public string Currency { get; set; }
    public int Amount { get; set; }
    public string Code { get; set; }
    public double Course { get; set; }

}

Upvotes: 3

Related Questions