0

I have the following javascript:

{
   "presenceDetected":true,
   "timestamp":1571219073514,
   "RegionMap":{
      "0":1,
      "1":0
   },
   "deviceId":" TEST_DEVICE_ID "
}

and my current C# MVC class:

[DataContract]
public class Presence
{
    [DataMember]
    public bool presenceDetected { get; set; }
    [DataMember]
    public long timestamp { get; set; }
    [DataMember]
    public Array RegionMap { get; set; }
    [DataMember]
    public string deviceId { get; set; }
}

And my API method:

public void MonitorPresence([FromBody] Presence APresenceData)
{
//Loop thru RegionMap here.
}

While this seems to bring in the array - I don't actually get any values. I am sure its wrong. There might be 0, 1, or 2+ values within the region map array.

I am not able to change the JSON - how do I build my class to accept the RegionMap array?

1
  • RegionMap is not an array it is an object in your json. Commented Sep 3, 2021 at 14:51

1 Answer 1

1

Fix your class, replace Array RegionMap with Dictionary<string,int>

[DataContract]
public class Presence
{
    [DataMember]
    public bool presenceDetected { get; set; }
    [DataMember]
    public long timestamp { get; set; }
    [DataMember]
    public Dictionary<string,string> RegionMap { get; set; }
    [DataMember]
    public string deviceId { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Dictionary<int,int> might be suitable as well
@PeterCsala Thank you. You are right but let us take the midle Dictionary<string,int>
The middle is how I think will work best - and it worked perfectly.

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.