I'm trying to do the following..
Not represent JSON in a POCO
Find and Deserialize only the value of a particular property hidden deep within a large JSON using Utf8JsonReader.
Filter for JSON tokens : working
Following is the code that is working.
using System;
using System.IO;
using System.Text;
using System.Text.Json;
namespace ProcessData
{
class Program
{
private static readonly byte[] s_pagetypeUtf8 = Encoding.UTF8.GetBytes("type");
private static readonly byte[] s_transactionorderidUtf8 = Encoding.UTF8.GetBytes("orderid");
private static readonly byte[] s_transactionorderamountUtf8 = Encoding.UTF8.GetBytes("amount_to_pay");
static void Main(string[] args)
{
string fileName = "D:\\500GBNP\\Projects\\temp1\\temp1.json";
using var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);
string line = String.Empty;
bool success =false;
string orderid="";
decimal orderamount=0;
while ((line = sr.ReadLine()) != null)
{
ReadOnlySpan<byte> jsonReadOnlySpan = Encoding.UTF8.GetBytes(line);
var reader = new Utf8JsonReader(jsonReadOnlySpan);
while (reader.Read())
{
JsonTokenType tokenType = reader.TokenType;
switch (tokenType)
{
case JsonTokenType.PropertyName:
if (reader.ValueTextEquals(s_pagetypeUtf8))
{
// Assume valid JSON, known schema
reader.Read();
if (reader.GetString().Equals("Success Confirmation"))
{
success=true;
}
}
break;
}
}
}
Console.WriteLine($"{success}");
}
}
}
The json file contains the following text and is successfully able to find if the line contains page type value Success Confirmation :
{"contexts":{"page":{"type":"Success Confirmation"},"transaction":{"orderid":100002210,"shipping_cost":70,"subtotal":200,"subtotal_incl_tax":200,"tax_cost":0,"amount_to_pay":270,"delivery":{"postcode":"400666","state":"West Bengal","city":"kolkata","country":"IN"},"billing":{"postcode":"400666","state":"West Bengal","city":"kolkata","country":"IN"},"product_details":{"no_of_items":1,"order_items":[{"name":"Designer Red Folral Earrings with White Beads","id":5384,"quantity":1,"price":200,"tax":0,"price_incl_tax":200,"actual_price":400,"percent_discount":50,"category":["New Arrivals","Modern Jewellery","Women","Earrings & Studs","Earrings & Studs","Danglers","View All","Hot Deals","Puja Collection 2016"]}]},"payment_mathod":"Cash On Delivery"}}
What I need to do is if page type is Success Confirmation, get the value of orderid and amount_to_pay. This is where I'm getting stuck.
I tried the following..
if (reader.GetString().Equals("Success Confirmation"))
{
success=true;
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
reader.Read();
if (reader.ValueTextEquals(s_transactionorderidUtf8))
{
reader.Read();
orderid=reader.GetString();
}
if (reader.ValueTextEquals(s_transactionorderamountUtf8))
{
reader.Read();
orderamount=reader.GetDecimal();
}
Console.WriteLine($"{orderid}:{orderamount}");
}
Without the if (reader.TokenType == JsonTokenType.EndObject) codeblock, it throws an error
> Cannot compare the value of a token type 'EndObject' to text.
With the code block it just moves forward
Any help is sincerely appreciated.
Please do ask if I have not been able to explain or you need any further info.
Thanks