[Solved] ASP.Net Deserialize JSON data Error: Type is not supported for deserialization of an array

afrozeameera
 
on Mar 25, 2021 12:47 AM
Sample_810511.zip
5455 Views

Getting Error while Deserialize JSON DATA (Type is not supported for deserialization of an array.)

var serializers = new System.Web.Script.Serialization.JavaScriptSerializer();
weatherAPI Weatherapi = new weatherAPI();
WebClient n = new WebClient();
var json = n.DownloadString("https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py?&request=execute&identifier=SinglePoint&parameters=PRECTOT&startDate=1981&endDate=2019&userCommunity=SSE&tempAverage=INTERANNUAL&outputList=JSON,CSV&lat=15.3173&lon=75.7139");
Weatherapi = serializers.Deserialize<weatherAPI>(json);

 

//CLASSES//
 
public class weatherAPI
{
    public List<subfeatur> features { get; set; }
    public subheader header { get; set; }
    public List<string> messages { get; set; }
    public suboutputs outputs { get; set; }
    public subparameterInformation parameterInformation { get; set; }
    public List<Dictionary<string, Double>> time { get; set; }
    public string type { get; set; }
}
 
public class subfeatur
{
    public subgeometry geometry { get; set; }
    public subproperties properties { get; set; }
    public string type { get; set; }
 
}
 
public class subgeometry
{
   
    public List<Double> coordinates { get; set; }
    public string type { get; set; }
}
 
 
public class subproperties
{
    public subparameter parameter { get; set; }
}
public class subparameter
{
    public sbPRECTOT1 PRECTOT { get; set; }
}
public class sbPRECTOT1
{
    public Dictionary<DateTime, Double> subclasses { get; set;}
 
}
 
public class suboutputs
{
    public string csv { get; set; }
    public string json { get; set; }
}
 
public class subheader
{
 
public string api_version { get; set; }
public string endDate { get; set; }
 
public string fillValue { get; set; }
 
public string startDate { get; set; }
 
public string title { get; set; }
 
}
public class subparameterInformation
{
    public sbPRECTOT PRECTOT { get; set; }
}
 
public class sbPRECTOT
{
    public string longname { get; set; }
    public string units { get; set; }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 26, 2021 01:06 AM

Hi afrozeameera,

Check this example. Now please take its reference and correct your code.

Namespaces

C#

using System.Net;
using System.Web.Script.Serialization;

VB.Net

Imports System.Net
Imports System.Web.Script.Serialization

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    WebClient n = new WebClient();
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    var json = n.DownloadString("https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py?&request=execute&identifier=SinglePoint&parameters=PRECTOT&startDate=1981&endDate=2019&userCommunity=SSE&tempAverage=INTERANNUAL&outputList=JSON,CSV&lat=15.3173&lon=75.7139");
    var serializers = new JavaScriptSerializer();
    Temperature temperature = serializers.Deserialize<Temperature>(json);
}

public partial class Temperature
{
    public Feature[] features { get; set; }
    public Header header { get; set; }
    public string[] messages { get; set; }
    public Outputs outputs { get; set; }
    public ParameterInformation parameterInformation { get; set; }
    public object[][] time { get; set; }
    public string type { get; set; }
}

public partial class Feature
{
    public Geometry geometry { get; set; }
    public Properties properties { get; set; }
    public string type { get; set; }
}

public partial class Geometry
{
    public double[] coordinates { get; set; }
    public string type { get; set; }
}

public partial class Properties
{
    public Parameter parameter { get; set; }
}

public partial class Parameter
{
    public Dictionary<string, double> PRECTOT { get; set; }
}

public partial class Header
{
    public string api_version { get; set; }
    public long endDate { get; set; }
    public long fillValue { get; set; }
    public long startDate { get; set; }
    public string title { get; set; }
}

public partial class Outputs
{
    public string csv { get; set; }
    public string json { get; set; }
}

public partial class ParameterInformation
{
    public Prectot PRECTOT { get; set; }
}

public partial class Prectot
{
    public string longname { get; set; }
    public string units { get; set; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim n As WebClient = New WebClient()
    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
    Dim json = n.DownloadString("https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py?&request=execute&identifier=SinglePoint&parameters=PRECTOT&startDate=1981&endDate=2019&userCommunity=SSE&tempAverage=INTERANNUAL&outputList=JSON,CSV&lat=15.3173&lon=75.7139")
    Dim serializers = New JavaScriptSerializer()
    Dim temperature As Temperature = serializers.Deserialize(Of Temperature)(json)
End Sub

Partial Public Class Temperature
    Public Property features As Feature()
    Public Property header As Header
    Public Property messages As String()
    Public Property outputs As Outputs
    Public Property parameterInformation As ParameterInformation
    Public Property time As Object()()
    Public Property type As String
End Class

Partial Public Class Feature
    Public Property geometry As Geometry
    Public Property properties As Properties
    Public Property type As String
End Class

Partial Public Class Geometry
    Public Property coordinates As Double()
    Public Property type As String
End Class

Partial Public Class Properties
    Public Property parameter As Parameter
End Class

Partial Public Class Parameter
    Public Property PRECTOT As Dictionary(Of String, Double)
End Class

Partial Public Class Header
    Public Property api_version As String
    Public Property endDate As Long
    Public Property fillValue As Long
    Public Property startDate As Long
    Public Property title As String
End Class

Partial Public Class Outputs
    Public Property csv As String
    Public Property json As String
End Class

Partial Public Class ParameterInformation
    Public Property PRECTOT As Prectot
End Class

Partial Public Class Prectot
    Public Property longname As String
    Public Property units As String
End Class

Screenshot