Solved

xamarin c# deserialize Json containing nested object

Posted on 2016-10-02
2
58 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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
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 and use switch statements in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now