0

I have is a list of properties, like public string RefID { get; set; } public bool myNiceProp { get; set; } and so on. I receive a string, and, some of those are in, for example "myNiceProp" = "1". First part of the problem, is to identify that I have the property "myNiceProp" and, if not, throw error, if yes, populate the value (and here is the second part of the problem, I must try to cast or somehow make sure the type is correct before I set the value to my property).I don't know which/how many key/value pairs are in the string.

Example:

public class InternationalSpaceStation
{
    public Location position { get; set; }
    public string missionName { get; set; }
    public int missionNumber { get; set; }
}

public class Location
{
    string coordinate { get; } = "Equatorial";
    string inclination { get; set; }
    string rightAscension { get; set; }
}

and my string looks like this:

{ "_report": { "ISS": { "inclination": "51.6430"

  "rightAscension": "53.5336"
},
"status": {
  "missionName": "Aquarius"
}   },   "ccId": "42NM", }

So, I don't really care for the "_report" because has no value, but I do wnt to find all nested pairs of keys and values and assign their value to my properties, if I have them (if not, to throw error). I don't care if i have more properties - but, less, is a problem.

I thought to try with String.Split but I also have the problem of the type, so I must try to convert the string's value type to mine (in string, all values are in quotes).

Any help would be immensely appreciated..

Nick
  • 483
  • 1
  • 6
  • 15
  • 2
    Please, share the json sample. Actually, you can use `JObject.Parse` method to parse json into key/values – Pavel Anikhouski Jan 09 '20 at 09:07
  • It sounds like you want to set properties of some object based on list of name/value pairs - since you already have code that iterates through values it looks like the only missing part is "how to set property by string name" which already answered (https://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value) before. If you asking something else - please [edit] question to clarify so someone can possibly re-open it. – Alexei Levenkov Jan 09 '20 at 09:22
  • @Pavel Anikhouski Thank you, I don't know if I can do it with JSON, I mean, I have a bunch of properties, but I don't know what key/value pairs the returning response has - so I had to search and if I find this property in my obj I populate (I have simple properties get/set) if not, I want to throw an error.. Apologies, I don't have source, I am trying it right now from scratch. – Nick Jan 09 '20 at 09:32
  • @Alexei Levenkov Thank you for trying this out, I don't have code, I now trying to implement (the above is from official doc). I will have a look on the link you sent, thank you very much! – Nick Jan 09 '20 at 09:33
  • @Nick you can always use `ContainsKey` method to see whether key exist in dictionary or not – Pavel Anikhouski Jan 09 '20 at 09:33
  • @Pavel Anikhouski: I would appreciate an example. So all I have is a list of properties, like `public string RefID { get; set; } public bool myNiceProp { get; set; }` and so on. I receive a string, and, some of those are in, for example `"myNiceProp" = "1"`. First part of the problem, is to identify that I have the property `"myNiceProp"` and, if not, throw error, if yes, populate the value (and here is the second part of the problem, I must try to cast or somehow make sure the type is correct before I set the value to my property).I don't know which/how many key/value pairs are in the string. – Nick Jan 09 '20 at 09:57
  • @Pavel Anikhouski: I think I understood, however, the thing that I must check the type (usually between string, number, and boolean) complicates the use of Dictionary.. – Nick Jan 09 '20 at 14:46
  • I gave a more precise description of the problem hoping for a solution.. – Nick Jan 09 '20 at 15:15
  • @Pavel Anikhouski: after consideration, seems dictionary is a difficult solution, I have duplicates. I was thinking how to split the string in an array and then try to see if I have this property (if the key has a value) and assign the value, else throw error with key and value.. – Nick Jan 09 '20 at 18:20

0 Answers0