0

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.

1
  • Welcome muchacho! Your Application class doesn't quite match up with the structure of your JSON. Commented Jan 3, 2019 at 23:10

2 Answers 2

1

your actual class is needed to be

public class Rootobject
{
    public string TransferResult { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public Application[] Applications { get; set; }
}

public class Application
{
    public string AppSerial { get; set; }
    public string OfficialResult { get; set; }
}

and you need to deserialize in this way

Rootobject app = JsonConvert.DeserializeObject<Rootobject>(json);
Sign up to request clarification or add additional context in comments.

1 Comment

Enlightning! How can I retrieve the values from the Application? Can you please post an example? Thank you!
0

Try the following:

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 List<ApplicationDetail> Applications { get; set; }
        } //Application

        public class ApplicationDetail
        {
            public string AppSerial { get; set; }
            public string OfficialResult { get; set; }
        } // ApplicationDetail

    } // Program
} //namespace

1 Comment

Brilliant! I was able to make it work using a foreach loop in the ApplicationDetail. foreach(var i in app.Applications) { Console.WriteLine($"AppSerial:{i.AppSerial} Result: {i.OfficialResult}"); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.