0
votes

im having trouble translating a json response from google's apis to DataContract classes. Specifically when trying to translate an int array from JSON to an int array in the DataContract.

Heres what I have done so far.

Heres the JSON response.

{
 "kind": "youtubeAnalytics#resultTable",
 "columnHeaders": [
  {
   "name": "views",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  },
  {
   "name": "likes",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  },
  {
   "name": "dislikes",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  }
 ],
 "rows": [
  [
   2162435,
   871,
   907
  ]
 ]
}

And heres my Data Contract classes

[DataContract]
public class AnalyticsData
{
    [DataMember(Name = "kind")]
    public string Kind { get; set; }

    [DataMember(Name = "columnHeaders")]
    public ColumnRows[] ColumnHeaders { get; set; }

    [DataMember(Name = "rows")]
    public int[][] rows { get; set; }  //<--------

}

[DataContract]
public class ColumnRows
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "columnType")]
    public string ColumnType { get; set; }

    [DataMember(Name = "dataType")]
    public string DataType { get; set; }
}

The problem I am having is translating the 'rows' element from the JSON doc into DataContract variables, as as shown in the AnalyticsData class in the int array 'rows'.

The error I get when debugging is

System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type StudioWP8.AnalyticsData. Input string was not in a correct format. ---> System.FormatException: Input string was not in a correct format.

1
you cannot use List<List<int>> instead of int[][]?hugo
Which serialization library are you using?Darin Dimitrov
Using DataContractJsonSerializer this works for me -- I don't get the exception. What serializer? What is the full traceback? Are these the actual classes & JSON that reproduce the bug, or are they simplifications?dbc
@dbc the serialization class i am using is System.Runtime.Serialization. Im calling the serilization by var analyticsData = await request.GetValueFromRequest<AnalyticsData>(); Debug.WriteLine(analyticsData.Rows[0][1]);Raajit Sharma
System.Runtime.Serialization is a namespace not a serializer. Can you edit your question to include the full traceback of the exception?dbc

1 Answers

0
votes

Use jsonconvert and it will be ok

        [TestMethod]
    public void test()
    {
        string json =
            "{ \"kind\": \"youtubeAnalytics#resultTable\", \"columnHeaders\": [  {   \"name\":\"views\",   \"columnType\": \"METRIC\",   \"dataType\": \"INTEGER\"  },  {   \"name\": \"likes\",   \"columnType\": \"METRIC\",   \"dataType\": \"INTEGER\" },  {   \"name\": \"dislikes\",   \"columnType\": \"METRIC\",   \"dataType\":\"INTEGER\"  } ], \"rows\": [  [   2162435,   871,907  ] ]}";

        AnalyticsData d = JsonConvert.DeserializeObject<AnalyticsData>(json);

    }