?
Solved

xamarin c# deserialize Json containing nested object

Posted on 2016-10-02
2
Medium Priority
?
589 Views
Last Modified: 2016-10-03
I have a webservice that return a Json in this format :
[{
    "Route0": {
        "RouteID": "AAA",
        "RouteDescription": "",
        "ReturnCode": "0",
        "ReturnError": ""
    }
}, {
    "Route1": {
        "RouteID": "AABCLO",
        "RouteDescription": "Antoine Abdo Bachaalani Close",
        "ReturnCode": "0",
        "ReturnError": ""
    }
}]

Open in new window


I need to deserialize it:  I created 2 class:

public class PullRouteDetailsObjectChild
    {
        public string RouteID { get; set; }
        public string RouteDescription { get; set; }
        public string ReturnCode { get; set; }
        public string ReturnError { get; set; }
    }

    public class PullRouteDetailsObject
    {
        public PullRouteDetailsObjectChild Route { get; set; }
    }

Open in new window

and I am using this code to deserialize:
    List<PullRouteDetailsObject> jsonRoutes =      
JsonConvert.DeserializeObject<List<PullRouteDetailsObject>>(jsonresult);

Open in new window


I am able to get a list of 2 PullRouteDetailsObject wich is correct but the child object is always null. I am sure that I am missing something but can't find what. I need to access child object Thank you for your help.
0
Comment
Question by:ProgrammerAtAIMS
2 Comments
 
LVL 35

Accepted Solution

by:
it_saige earned 2000 total points
ID: 41826976
This is because your format is returning a List of Dictionary values; Example:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EE_Q28973879
{
	class Program
	{
		static string json = "[{ \"Route0\": { \"RouteID\": \"AAA\", \"RouteDescription\": \"\", \"ReturnCode\": \"0\", \"ReturnError\": \"\" }}, { \"Route1\": { \"RouteID\": \"AABCLO\", \"RouteDescription\": \"Antoine Abdo Bachaalani Close\", \"ReturnCode\": \"0\", \"ReturnError\": \"\" }}]";

		static void Main(string[] args)
		{
			var parsed = JArray.Parse(json);
			var routes = JsonConvert.DeserializeObject<Routes>(json);
			foreach (var route in routes)
				foreach (var pair in route)
					Console.WriteLine("Name: {0}; Route: {1}", pair.Key, pair.Value);
			Console.ReadLine();
		}
	}

	class Routes : List<Dictionary<string, Route>>
	{
		public Routes() { ;}
	}

	class Route
	{
		[JsonProperty("RouteID")]
		public string ID { get; set; }
		[JsonProperty("RouteDescription")]
		public string Description { get; set; }
		[JsonProperty("ReturnCode")]
		public string Code { get; set; }
		[JsonProperty("ReturnError")]
		public string Error { get; set; }

		public override string ToString()
		{
			return string.Format("ID: {0}; Description: {1}; Code: {2}; Error: {3}", ID, Description, Code, Error);
		}
	}
}

Open in new window

Produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:ProgrammerAtAIMS
ID: 41827486
Ok thank you @it_saige that worked but (the dictionary thing) .
I used:
var jsonRoutes = JsonConvert.DeserializeObject<Dictionary<object, List<PullRouteDetailsObjectChild>>>(jsonresult);

Open in new window

and for reading:
 foreach (object key in jsonRoutes.Keys)
                    {
                        foreach (PullRouteDetailsObjectChild current_route in jsonRoutes[key])
                        {
                            string RouteID = current_route.RouteID;
                            Routes.insertRoute(RouteID);
                         }

                    }

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question