0

I have an array of json objects like this:

[{
    "Time-VitalsTime": "2019-06-22T01:01:00",
    "TimeToEstimate-VitalsTime": 10,
    "TimeToEstimate-1unit": 5,
    "TimeToEstimate-2units": 9,
    "TimeToEstimate-3units": 21,
    "TimeToEstimate-1week": 13,
    "TimeToEstimate-2weeks": 16,
    "TimeToEstimate-3weeks": 20
}, {
    "Time-VitalsTime": "2019-06-22T01:02:00",
    "TimeToEstimate-VitalsTime": 8,
    "TimeToEstimate-1unit": 10,
    "TimeToEstimate-2units": 5,
    "TimeToEstimate-3units": 9,
    "TimeToEstimate-1week": 18,
    "TimeToEstimate-2weeks": 15,
    "TimeToEstimate-3weeks": 32
}, {
    "Time-VitalsTime": "2019-06-22T01:03:00",
    "TimeToEstimate-VitalsTime": 10,
    "TimeToEstimate-1unit": 8,
    "TimeToEstimate-2units": 10,
    "TimeToEstimate-3units": 5,
    "TimeToEstimate-1week": 15,
    "TimeToEstimate-2weeks": 14,
    "TimeToEstimate-3weeks": 16
}, 

And I need to get it into this shape (according to the api documentation):

{
"data":
    [
        <model-specific-data-structure>
    ]
}

The code used to generate the string above is:

public string ConvertCsvFileToJsonObject(string path)
{
    var csv = new List<string[]>();
    var lines = File.ReadAllLines(path);

    foreach (string line in lines)
        csv.Add(line.Split(','));

    var properties = lines[0].Split(',');

    var listObjResult = new List<Dictionary<string, string>>();

    for (int i = 1; i < lines.Length; i++)
    {
        var objResult = new Dictionary<string, string>();
        for (int j = 0; j < properties.Length; j++)
            objResult.Add(properties[j], csv[i][j]);

        listObjResult.Add(objResult);
    }

    var newlistObjResult = new List<Dictionary<dynamic,dynamic>>();
    var count = 0;

    foreach (var dictionary in listObjResult)
    {
        Console.WriteLine($"Dictionary I am currently on: {count}");

        var newDict = new Dictionary<dynamic, dynamic>();
        foreach (var item in dictionary)
        {
            DateTime dateTime;
            if (DateTime.TryParse(item.Value, out dateTime))
            {
                newDict.Add(item.Key, dateTime);
                continue;
            }
            else
            {
                try
                {
                    var asInt = Convert.ToInt32(item.Value);
                    newDict.Add(item.Key, asInt);
                }
                catch (Exception e)
                {
                    var ex = e.Message;
                }
            }
        }
        newlistObjResult.Add(newDict);
        count += 1;
    }           
    var result2 = JsonConvert.SerializeObject(newlistObjResult);
    return result2;
}

Then I'm using this:

 request.Content = new StringContent(JsonConvert.SerializeObject(stringgeneratedabove));

to serialize it to send to a REST api. The error I am getting back from the call is: "{\"error\": \"string indices must be integers\"}"

A friend said that I need to pass the array in a property named "data". After doing a fair amount of searching online, I do not know where to start. I tried doing something like this:

[DataContract]
public class CsvDataClass
{
    [DataMember]
    public List<Dictionary<object, object>> CsvData { get; set; }

    [DataMember]
    public Dictionary<object, object>[] Data
    {
        get { return CsvData.ToArray(); }
    }
}

based on what a friend said, but couldn't get it implemented. I've searched for "changing the shape of json c#" or "passing json array into a property", and have also read general documentation about json objects, but nothing has clicked yet.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • If you have a `List> list`, you can just construct a wrapper anonymous object like so: `new { data = list };` and serialize that. Is that what you want? – dbc Aug 10 '19 at 21:03
  • Not sure, I'll look at that. Meanwhile, I did edit the post. Thanks for helping. – redmondcoffehead Aug 10 '19 at 21:15
  • 1) Can you share the ``? 2) Don't forget to set the encoding and content type on `StringContent`, e.g. `StringContent(myJson,Encoding.UTF8,"application/json")`, as shown in [POSTing JsonObject With HttpClient From Web API](https://stackoverflow.com/a/39414248) or [How do you set the Content-Type header for an HttpClient request?](https://stackoverflow.com/a/10679340). – dbc Aug 10 '19 at 21:33
  • the model specific data structure is the array of json objects. Thanks for the reminder about Encoding.UTF8, that was part of the problem! – redmondcoffehead Aug 10 '19 at 22:02

1 Answers1

1

To get the json into the shape required, I did this:

var result = "{\"data\":" + jsonString + "}";

where jsonString equals the array of objects shown at the top of this thread, so I just added those values manually.

And I also had to change this:

request.Content = new StringContent(JsonConvert.SerializeObject(result))
request.Content.Headers.ContentType = new 
MediaTypeHeaderValue("application/json");

to:

request.Content = new StringContent(asjson.ToString(), Encoding.UTF8, "application/json");