Solved

xamarin c# deserialize Json containing nested object

Posted on 2016-10-02
2
139 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 33

Accepted Solution

by:
it_saige earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
https://github.com/willowsystems/jSignature 11 109
Constant string is of type char *   ? 7 48
Test the speeds on my PC Drives 12 71
SP result not being displayed 5 36
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

685 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