Unable to parse mixed key/value pairs with arrays in the middle. I can either get only the key/values or only get the array but not both.
I tried following the documentation at https://www.newtonsoft.com/json/help/html/SerializingJSON.htm but did not find an example that matches my need.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ParsingJSON
{
class Program
{
static void Main(string[] args)
{
// code to deserialize from JSON string to a typed object
string json = @"{
'TransferResult': 'SUCCESS',
'City': 'California',
'State': 'CA',
'Applications': [
{'AppSerial' : 'APX3531'},
{'AppSerial' : 'APX3263'},
{'AppSerial' : 'APX3251','OfficialResult' : 'PENDING'},
{'AppSerial' : 'APX3228'},
{'AppSerial' : 'APX9521'},
{'AppSerial' : 'APX3251','OfficialResult' : 'APPROVED'},
]}";
Application app = JsonConvert.DeserializeObject<Application>(json);
if(app.TransferResult == "SUCCESS")
{
// if TransferResult == SUCCESS
// grab the City, State, AppSerial, and OfficialResult if any
Console.WriteLine(app.City);
Console.WriteLine(app.State);
Console.WriteLine(app.AppSerial); // make key value pairs of AppSerial and values and OfficialResults and results
Console.WriteLine(app.OfficialResult); // if any
Console.ReadLine();
}
} // Main
public class Application
{
public string TransferResult { get; set; } //SUCCESS or FAIL
public string City { get; set; } // California.
public string State { get; set; } // CA
public string AppSerial { get; set; } // APX12345
public string OfficialResult { get; set; } // APPROVED, PENDING, ADOPTED
} //Application
} // Program
} //namespace
I would like to get the JSON City, State, and the AppSerials for the City along with the OfficialResult if any. Right now I'm getting just the City, State, and nothing for the AppSerials.
Applicationclass doesn't quite match up with the structure of your JSON.