I have the following sctructure JSON data.
Json Data:
{
"items" : [ {
"downloadUrl" : "XXXX",
"path" : "XXXX",
"id" : "XXXX",
"repository" : "XXXX",
"format" : "XXXX",
}, {
"downloadUrl" : "XXXX",
"path" : "XXXX",
"id" : "XXXX",
"repository" : "XXXX",
"format" : "XXXX",
}, {
"downloadUrl" : "XXXX",
"path" : "XXXX",,
"id" : "XXXX",
"repository" : "XXXX",
"format" : "XXXX",
} ],
"continuationToken" : "YYYY"
}
I need to get the item value in List (List<repository>
). How can I retrieve the Values?
downloadUrl,path,id,repository,format,continuoationToken
I have try to get below like this.
class Program
{
public class repository
{
public NpmPackages items { get; set; }
}
public class NpmPackages
{
[JsonProperty(PropertyName = "downloadUrl")]
public string DownloadUrl { get; set; }
[JsonProperty(PropertyName = "path")]
public string Path { get; set; }
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "repository")]
public string Repository { get; set; }
[JsonProperty(PropertyName = "format")]
public string Format { get; set; }
}
public static string HttpOperation(string apiUrl)
{
if (string.IsNullOrEmpty(apiUrl) == true)
{
throw new ArgumentNullException(nameof(apiUrl));
}
Uri url = new Uri(apiUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
static void Main(string[] args)
{
List<repository> packageDetails = new List<repository>();
List<repository> packageData = new List<repository>();
var jsonData = HttpOperation("XXXX");
packageData = JsonConvert.DeserializeObject<List<repository>>(jsonData);
packageDetails.AddRange(packageData);
}
}
While try like this I am facing below error.
Error Details:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[npm_clean_up_Tool.Program+NpmPackages]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'items', line 2, position 11.
please update your suggestions to achieve my goal.