Solved

Help Parsing JSON to C# class

Posted on 2014-10-23
5
357 Views
Last Modified: 2014-11-12
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

0
Comment
Question by:wint100
5 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
 
LVL 1

Author Comment

by:wint100
Comment Utility
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
 
LVL 1

Author Comment

by:wint100
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 1

Accepted Solution

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

14 Experts available now in Live!

Get 1:1 Help Now