Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help Parsing JSON to C# class

Posted on 2014-10-23
5
Medium Priority
?
508 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 84

Expert Comment

by:David Johnson, CD, MVP
ID: 40401729
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
ID: 40401734
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
ID: 40401751
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 75

Expert Comment

by:käµfm³d 👽
ID: 40402747
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 2000 total points
ID: 40406313
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

564 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