0

I have a json file that looks like this:

[
    [value1, value2, value3]
    [value2, value2, value3]
    ...
]

I've tried to use both

using (StreamReader r = new StreamReader(HostingEnvironment.MapPath("~/Content/TempData/CapitalSparesBucketsTableData.json")))
        {
            string json = r.ReadToEnd();
            JavascriptSerialzer ser = new JavaScriptSerializer()
            var r  = ser.Deserialize<ResultList>(json);

        }

and

  using (StreamReader r = new StreamReader(HostingEnvironment.MapPath("~/Content/TempData/searchData.json")))
        {
            string json = r.ReadToEnd();
            List<SearchResult> searchResults = JsonConvert.DeserializeObject<List<SearchResult>>(json);
        }

and I get errors using both methods.

My model looks like this:

public class SearchResults
{
    public int TotalRecords { get; set; }
    public SearchResult[] Results { get; set; }
}

public class ResultList
{
    public SearchResult record;
}



public class SearchResult
    {
        public SearchResult() { }
        public SearchResult(IDataReader reader)
    {
        DataTable dt = new DataTable();

        using (reader)
        {
            dt.Load(reader);
        }
        DataRow row = dt.Rows[0];
    }

    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
    public string Value6 { get; set; }
}

Any help would be appreciated.

TIA

slugster
  • 49,403
  • 14
  • 95
  • 145
Rob M
  • 1,007
  • 2
  • 17
  • 38
  • That doesn't look like a JSON file. – Dan Wilson Jun 06 '19 at 20:21
  • 1
    Please provide a plausible json example – Fabio Jun 06 '19 at 20:24
  • It's valid. Unusual, but valid. https://stackoverflow.com/questions/5034444/can-json-start-with – rogerdossantos Jun 06 '19 at 20:51
  • Try to put break points and find out exactly which line is giving the error, and please provide the specific error text. – DaniDev Jun 06 '19 at 21:08
  • @Fabio it is a valid json. It's what I have to work with. If it was in the normal key: value pair format, this would be a non issue. – Rob M Jun 06 '19 at 23:39
  • Hi, I've edited your title. I know why you did it, but please don't use tags to disambiguate the title - look for other ways to make it distinct. Thanks :) – slugster Jun 06 '19 at 23:52
  • @RobM it's valid without comma? From model i assume the values are strings, so he needs quotation marks. If he get errors could be the json is bad formatted – Fabio Jun 07 '19 at 08:22
  • @slugster maybe I didn't understand correctly, but if you want a model you should have keys / values, otherwise you only have lists in lists. – Fabio Jun 07 '19 at 08:24

1 Answers1

1

Hmm, looks like a Jagged Array. I tried this and it worked with .Net Core 2.2.

var result = JsonConvert.DeserializeObject<IEnumerable<IEnumerable<string>>>("[['value'],['value']]");

Obs.: couldn't find any question that is exactly like this question.

rogerdossantos
  • 161
  • 1
  • 10
  • Thanks. This worked. now I just need to massage it into a model. – Rob M Jun 06 '19 at 23:49
  • Nice work spartanroger . I am still curious though why desalinizing directly to the model did not work. Maybe @Rob M you can still provide us with the error message so that we can help serialize into the model. I am thinking that you need to provide values for each of your model properties – DaniDev Jun 07 '19 at 23:00