2

I'm struggling with this function i've been searching for awhile but for some reason it can't get the outcome i desire. I have a stored procedure which returns the following data. Query Results Img

Here is the class i'm using for the list:

 [DataContract]
public class EvtTypes
{
    [DataMember]
    public string EvtType { get; set; }
    [DataMember]
    public int EvtCnt { get; set; }
} 

Here is the code the makes the call:

[OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "getEvtTypesCnt")]
    List<EvtTypes> GetEvtTypesCnt();

public List<EvtTypes> GetEvtTypesCnt()
    {
        var data = new StHomeDBDataContext();
        var list = new List<EvtTypes>();
        foreach (var r in data.GetAllEvtTypesCnt_ToList())
        {
            list.Add(new EvtTypes
            {
                EvtType = r.evtType,
                EvtCnt = Convert.ToInt32(r.evtCnt)
            });

        }
        return list;
    }

which when called returns this:

[
  {
    "EvtCnt": 18,
    "EvtType": "alarmSystemStatus"
  },
  {
    "EvtCnt": 13871,
    "EvtType": "battery"
  },
  {
    "EvtCnt": 210,
    "EvtType": "button"
  },
  {
    "EvtCnt": 23,
    "EvtType": "color"
  },
  {
    "EvtCnt": 3777,
    "EvtType": "contact"
  },
  {
    "EvtCnt": 88,
    "EvtType": "door"
  },
  {
    "EvtCnt": 103440,
    "EvtType": "energy"
  },
  {
    "EvtCnt": 304,
    "EvtType": "heatingSetpoint"
  },
  {
    "EvtCnt": 24,
    "EvtType": "hue"
  }
]

I really want to format it to look like this:

{
    "evtType": [{
        "name": "name here",
        "cnt": 2323
    }]
}

2 Answers 2

1

You would need object of following structure:

public class EvtType
{
    public string Name { get; set; }
    public int Cnt { get; set; }
}

public class RootObject
{    
    public List<EvtType> EvtType { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

It sounds like this is a serialization question. You can customize the names and order the properties using the DataContract/DataMember attributes:

[DataContract]
public class EvtTypes
{
    [DataMember(Name="cnt", Order=1)]
    public string EvtType { get; set; }
    [DataMember(Name="name", Order=2)]
    public int EvtCnt { get; set; }
} 

By default, JSON serialization uses the name of your property as the field name in the JSON object. You can override that using the Name property of DataMember. Also, the default sort order for field names seems to be alphabetic order. You can override that by specifying the Order property on DataMember.

JQuery doesn't care about the order of the field names of your JSON object and you can access them directly. You may choose not to worry about field order.

To get the rest of the JSON structure you want, just keep in mind that the {} notation is a map. You can get that by encapsulating it like this:

Dictionary<string,List<EvtType>> map = new Dictionary<string,List<EvtType>>();
map.Add("evtType", list);

Don't forget to change the return type for you method.

The full replacement for your web service would be:

[OperationContract]
[WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "getEvtTypesCnt")]
Dictionary<string,EvtTypes> GetEvtTypesCnt();

public Dictionary<string,EvtTypes> GetEvtTypesCnt()
{
    var data = new StHomeDBDataContext();
    var list = new List<EvtTypes>();
    foreach (var r in data.GetAllEvtTypesCnt_ToList())
    {
        list.Add(new EvtTypes
        {
            EvtType = r.evtType,
            EvtCnt = Convert.ToInt32(r.evtCnt)
        });

    }

    Dictionary<string,List<EvtType>> map =
       new Dictionary<string,List<EvtType>>();
    map.Add("evtType", list);

    return map;
}

If I understood your intent properly.

4 Comments

Thanks for your reply... I'm having a lot of trouble getting this parsed out in Groovy for my smartthings app. I don't have issues with other JSON.
'[ { "Key": "evtType", "Value": [ { "name": "alarmSystemStatus", "cnt": 18 }, { "name": "battery", "cnt": 13936 }, { "name": "button", "cnt": 210 }, { "name": "color", "cnt": 23 }, { "name": "contact", "cnt": 3782 }, { "name": "door", "cnt": 88 }, { "name": "energy", "cnt": 103963 } ] } ]' I cannot get this to parse into Groovy... I'm not sure what the issue is but it's become very annoying
Groovy is a Java based language, while I'm well versed in C# serialization, I'm not as familiar with the Java libraries. Just remember that {key: value} is notation for a map (HashMap) and the [element,element] notation is for a list. If you run out of options with the Groovy libraries, you can always simply repack the data in simple collections as a fallback.
Also, why did you pack it in another list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.