[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

xamarin c# deserialize Json containing nested object

Posted on 2016-10-02
2
Medium Priority
?
482 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

650 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