VInod Mosiganti

VInod Mosiganti

  • NA
  • 18
  • 1.2k

JSON Response Error

May 20 2020 6:46 AM
HI Team,
I am trying to get JSON data by http url and then write Json Data console but geting bellow error: 
 
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Trail05
StackTrace:
at ReadCountryData.Program.Main(String[] args) in D:\CSOM_Dev\ENRICH TRAIL\Trail05\Trail05\Program.cs:line 45
Below is the Request URL:
https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json
Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
namespace ReadCountryData
{
class Program
{
public class positionsobj
{
public positionlist positions { get; set; }
}
public class positionlist
{
public List position { get; set; }
}
public class position
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
}
static void Main(string[] args)
{
HttpClient http = new HttpClient();
string url = "https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json";
HttpResponseMessage response = http.GetAsync(new Uri(url)).Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseBody);
positionsobj position = JsonConvert.DeserializeObject(responseBody);
foreach (position item in position.positions.position)
{
Console.WriteLine("{0}", item.CustomerID);
}
Console.ReadLine();
}
}
}

Answers (3)