Help Parsing JSON to C# class

Hi,

I've typically used json2charp to help convert JSON responses to a C# class, but this new response it proving tricky.

The response from the service is below, this is actually pulled from the service developers documentation. I can't seem to work out how to use JSON.NET to parse this response to a C' class.

Can anyone work this out?

{
    "$base": "Collection",
    "analog-input,1": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU VFD Speed Fback",
        "truncated": "true"
    },
    "nodeType": "Device",
    "displayName": "PAC Lab 1 AHU",
    "next": "http://192.168.150.137/enteliweb/api/.bacnet/MainSite/10100?skip=15&max-results=15",
    "analog-input,2": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU PREC T",
        "truncated": "true"
    },
    "analog-input,3": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU Supply Air Temp",
        "truncated": "true"
    },
    "analog-input,5": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU Room Temp 1",
        "truncated": "true"
    },
    "analog-input,6": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU Room Temp 2",
        "truncated": "true"
    },
    "analog-input,7": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU Flow",
        "truncated": "true"
    },
    "analog-input,8": {
        "$base": "Object",
        "displayName": "PAC_Lab_1_AHU DP",
        "truncated": "true"
    },
    "analog-input,10": {
        "$base": "Object",
        "displayName": "PAC Lab 1 EF Speed FB",
        "truncated": "true"
    },
    "analog-input,11": {
        "$base": "Object",
        "displayName": "PAC Lab 1 EF Flow",
        "truncated": "true"
    },
    "aic,4": {
        "$base": "Object",
        "displayName": "Current 0-5V 0-10 A AIC",
        "truncated": "true"
    },
    "aic,11": {
        "$base": "Object",
        "displayName": "Temperature 50-100F AIC",
        "truncated": "true"
    },
    "aic,18": {
        "$base": "Object",
        "displayName": "Pressure 4-20mA 0-500 Pa AIC",
        "truncated": "true"
    },
    "aic,19": {
        "$base": "Object",
        "displayName": "Temperature 30-120F AIC",
        "truncated": "true"
    },
    "aic,20": {
        "$base": "Object",
        "displayName": "Temperature 30-60F AIC",
        "truncated": "true"
    },
    "aic,21": {
        "$base": "Object",
        "displayName": "Temperature 10K -35-240 degF AIC",
        "truncated": "true"
    }
}

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
I tried to convert it to xml but failed
<?xml version="1.0" encoding="UTF-8" ?>

<root>
	<$base>Collection
</$base>
	
<analog-input,1>
	<$base>Object
</$base>
	
<displayName>PAC_Lab_1_AHU VFD Speed Fback</displayName>

	<truncated>true</truncated>
	undefined</analog-input,1>
	undefined
	<nodeType>Device</nodeType>
	undefined
	<displayName>PAC Lab 1 AHU
	</displayName>undefined
	<next>http://192.168.150.137/enteliweb/api/.bacnet/MainSite/10100?skip=15&max-results=15</next>
	undefined
	<analog-input,2>
	<$base>Objectundefined
	</$base>undefined<displayName>PAC_Lab_1_AHU PREC T</displayName>
	undefined<truncated>true</truncated>undefined
	</analog-input,2>undefined<analog-input,3>
	<$base>Objectundefined</$base>undefined
	<displayName>PAC_Lab_1_AHU Supply Air Temp</displayName>
	undefined<truncated>true</truncated>
	undefined</analog-input,3>undefined<analog-input,5><$base>Objectundefined</$base>undefined<displayName>PAC_Lab_1_AHU Room Temp 1</displayName>undefined<truncated>true</truncated>undefined</analog-input,5>undefined<analog-input,6><$base>Objectundefined</$base>undefined<displayName>PAC_Lab_1_AHU Room Temp 2</displayName>undefined<truncated>true</truncated>undefined</analog-input,6>undefined<analog-input,7><$base>Objectundefined</$base>undefined<displayName>PAC_Lab_1_AHU Flow</displayName>undefined<truncated>true</truncated>undefined</analog-input,7>undefined<analog-input,8><$base>Objectundefined</$base>undefined<displayName>PAC_Lab_1_AHU DP</displayName>undefined<truncated>true</truncated>
	undefined</analog-input,8>undefined<analog-input,10>	
	<$base>Objectundefined</$base>undefined<displayName>PAC Lab 1 EF Speed FB</displayName>undefined<truncated>true</truncated>undefined</analog-input,10>undefined<analog-input,11><$base>Objectundefined</$base>undefined<displayName>PAC Lab 1 EF Flow</displayName>undefined<truncated>true</truncated>undefined</analog-input,11>undefined<aic,4><$base>Objectundefined</$base>undefined<displayName>Current 0-5V 0-10 A AIC</displayName>undefined<truncated>true</truncated>undefined</aic,4>undefined<aic,11><$base>Objectundefined</$base>undefined<displayName>Temperature 50-100F AIC</displayName>undefined<truncated>true</truncated>undefined</aic,11>undefined<aic,18><$base>Objectundefined</$base>undefined<displayName>Pressure 4-20mA 0-500 Pa AIC</displayName>undefined<truncated>true</truncated>undefined</aic,18>undefined<aic,19><$base>Objectundefined</$base>undefined<displayName>Temperature 30-120F AIC</displayName>undefined<truncated>true</truncated>undefined</aic,19>undefin	ed<aic,20><$base>Objectundefined</$base>undefined<	displayName>Temperature 30-60F AIC</displayName>undefined<truncated>true</truncated>undefined</aic,20>undefined<aic,21><$base>Objectundefined</$base>undefined<displayName>Temperature 10K -35-240 degF AIC</displayName>undefined<truncated>true</truncated>undefined</aic,21>
	undefined
</root>

Open in new window

since JSON has items that XML 2.0 doesn't understand but supposedly XSLT 3.0 http://www.w3.org/TR/xslt-30/#json uses maps which will help


 public class AnalogInput1
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput2
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput3
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput5
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput6
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput7
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput8
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput10
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class AnalogInput11
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic4
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic11
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic18
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic19
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic20
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Aic21
    {
        public string $base { get; set; }
        public string displayName { get; set; }
        public string truncated { get; set; }
    }

    public class Example
    {
        public string $base { get; set; }
        public AnalogInput1 analog-input,1 { get; set; }
        public string nodeType { get; set; }
        public string displayName { get; set; }
        public string next { get; set; }
        public AnalogInput2 analog-input,2 { get; set; }
        public AnalogInput3 analog-input,3 { get; set; }
        public AnalogInput5 analog-input,5 { get; set; }
        public AnalogInput6 analog-input,6 { get; set; }
        public AnalogInput7 analog-input,7 { get; set; }
        public AnalogInput8 analog-input,8 { get; set; }
        public AnalogInput10 analog-input,10 { get; set; }
        public AnalogInput11 analog-input,11 { get; set; }
        public Aic4 aic,4 { get; set; }
        public Aic11 aic,11 { get; set; }
        public Aic18 aic,18 { get; set; }
        public Aic19 aic,19 { get; set; }
        public Aic20 aic,20 { get; set; }
        public Aic21 aic,21 { get; set; }
    }

Open in new window

So I tried this http://jsonutils.com/
0
wint100Author Commented:
Thanks, I got a similar output from json2csharp but my issue is that the elements are dynamic and the Aic and analoginput elements may have different names suck as binaryinput or Bdc or Aoc.

I'm really not sure how to tackle this. What should happen, is a request is made to get a list of type from the server and the Json as in the example ebove will be returned with a list of Objects (Object being Aic, AnalogInput etc..) with the Instance and details of each.

I may have to rely on manually parsing each and every request.
0
wint100Author Commented:
The service can also return XML, but I was hoping to move with JSON. The XML response is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Collection nodeType="Device" displayName="PAC Lab 1 AHU" name="10100" next="http://192.168.150.137/enteliweb/api/.bacnet/MainSite/10100?skip=15&amp;max-results=15"
    xmlns="http://bacnet.org/csml/1.2">
    <Object name="analog-input,1" displayName="PAC_Lab_1_AHU VFD Speed Fback" truncated="true"/>
    <Object name="analog-input,2" displayName="PAC_Lab_1_AHU PREC T" truncated="true"/>
    <Object name="analog-input,3" displayName="PAC_Lab_1_AHU Supply Air Temp" truncated="true"/>
    <Object name="analog-input,5" displayName="PAC_Lab_1_AHU Room Temp 1" truncated="true"/>
    <Object name="analog-input,6" displayName="PAC_Lab_1_AHU Room Temp 2" truncated="true"/>
    <Object name="analog-input,7" displayName="PAC_Lab_1_AHU Flow" truncated="true"/>
    <Object name="analog-input,8" displayName="PAC_Lab_1_AHU DP" truncated="true"/>
    <Object name="analog-input,10" displayName="PAC Lab 1 EF Speed FB" truncated="true"/>
    <Object name="analog-input,11" displayName="PAC Lab 1 EF Flow" truncated="true"/>
    <Object name="aic,4" displayName="Current 0-5V 0-10 A AIC" truncated="true"/>
    <Object name="aic,11" displayName="Temperature 50-100F AIC" truncated="true"/>
    <Object name="aic,18" displayName="Pressure 4-20mA 0-500 Pa AIC" truncated="true"/>
    <Object name="aic,19" displayName="Temperature 30-120F AIC" truncated="true"/>
    <Object name="aic,20" displayName="Temperature 30-60F AIC" truncated="true"/>
    <Object name="aic,21" displayName="Temperature 10K -35-240 degF AIC" truncated="true"/>
</Collection>

Open in new window

0
käµfm³d 👽Commented:
You won't be able to convert those directly because comma is not valid in an identifier in C#. I would say to just use the json2csharp as you have been, and then manually change the invalid names--probably just use the original name sans comma. Depending on the functionality of the (de)serializer you use--Newtonsoft's is pretty good--you may be able to add attributes to each property with the modified name such that the serializer can still map the data to the property, even though the names won't match.
0
Ajay MahajanSr. Software DeveloperCommented:
Hello,

You can do once thing, Follow the below code and you will receive generic result, on that result you can iterations and get the result in the form you want.

you have to add reference for System.Net.json to achieve this, i have not taken whole JSON what you have specified. also attached image which show you the PARSED result for this JSON string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Json;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            JsonTextParser jSonParser = new JsonTextParser();
            JsonObject jSONResult =  jSonParser.Parse("{\"$base\": \"Collection\",\"analog-input,1\": {\"$base\": \"Object\", \"displayName\": \"PAC_Lab_1_AHU VFD Speed Fback\",\"truncated\": \"true\"},\"nodeType\": \"Device\",\"displayName\": \"PAC Lab 1 AHU\",\"next\": \"http://192.168.150.137/enteliweb/api/.bacnet/MainSite/10100?skip=15&max-results=15\",\"analog-input,2\": {\"$base\": \"Object\",\"displayName\": \"PAC_Lab_1_AHU PREC T\",\"truncated\": \"true\"}}");
         }
    }
}

Open in new window



SolutionImage.pngSolutionImage.png
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.