?
Solved

xamarin c# deserialize Json containing nested object

Posted on 2016-10-02
2
Medium Priority
?
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

770 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