Tag Archives: Deserializing Json Array using Newtonsoft.Json

C#: Deserializing Json Array using Newtonsoft.Json

Deserializing Json Array using Newtonsoft.Json

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ABJNewtonsoftJson
{
public class Program
{
static void Main(string[] args)
{
string json = @”{‘lines’:[
{‘RecId’:35637332944,’LineNumber’:1,’ItemId’:’VH001′,’ReceivedQty’:2},
{‘RecId’:35637332945,’LineNumber’:2,’ItemId’:’VH002′,’ReceivedQty’:6},
{‘RecId’:35637332946,’LineNumber’:3,’ItemId’:’VH003′,’ReceivedQty’:8},
{‘RecId’:35637332947,’LineNumber’:4,’ItemId’:’VH004′,’ReceivedQty’:9},
{‘RecId’:35637332948,’LineNumber’:5,’ItemId’:’VH005′,’ReceivedQty’:1}]}”;
//PurchLineData purchLineData = JsonConvert.DeserializeObject<List<PurchLineData>>(json);

PurchLineDataList purchLineDataList = JsonConvert.DeserializeObject<PurchLineDataList>(json);

for (var i = 0; i < purchLineDataList.lines.Count; i++)
{
Console.WriteLine(“RecId : {0}”, purchLineDataList.lines[i].RecId);
Console.WriteLine(“LineNumber : {0}”, purchLineDataList.lines[i].LineNumber);
Console.WriteLine(“ItemId : {0}”, purchLineDataList.lines[i].ItemId);
Console.WriteLine(“ReceivedQty : {0}”, purchLineDataList.lines[i].ReceivedQty);
Console.WriteLine(‘\n’);
}
Console.ReadLine();
}
}

public class PurchLineDataList
{
public List<PurchLineData> lines;
}

public class PurchLineData
{
public Int64 RecId { get; set; }
public int LineNumber { get; set; }
public string ItemId { get; set; }
public int ReceivedQty { get; set; }
}
}

Advertisements