0

I'm working on a Xamarin App, and I use Newtonsoft for Json. But I'm having trouble with processing some data that I get back.

{
"ok": true,
"payment-methods": [
     {
     "id": "39sahf92ka9s02",
         "type": "ideal",
            "options": {
                "issuers": {
                    99: "Test Issuer"
                }
            }
        }
    ],
}

I don't know how to get to the Test Issuer, because the Key value could be any integer.

A Dictionary makes a lot of sense to use, but then I get the following exception: "System.NullReferenceException: Object reference not set to an instance of an object. json"

I have the following as Model:

[JsonObject(MemberSerialization.OptIn)]
public class PaymentOptions
{
    [JsonProperty("ok")]
    public Boolean OK { get; set; }
    [JsonProperty("payment-methods")]
    public List<PaymentMethods> PaymentMethods { get; set; }
}
public class PaymentMethods
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("type")]
    public string Type { get; set; }
    [JsonProperty("options")]
    public Options Options { get; set; }
}
public class Options
{
    [JsonProperty("issuers")]
    public IDictionary<int, string> Issuers { get; set; }
}

I deserialize the Json through the following:

var deserializedGetPaymentOptions = JsonConvert.DeserializeObject<Models.PaymentMethods>(await responseGetPaymentOptions.Content.ReadAsStringAsync());

And after that I try to read it by using it in a foreach loop:

foreach (KeyValuePair<int, string> issuerFromDict in deserializedGetPaymentOptions.Options.Issuers)

JeroenM
  • 807
  • 1
  • 11
  • 26

2 Answers2

0

Have you tried with 99 as string in the json representation ?

...
"issuers": {
    "99": "Test Issuer"
}
...

You should let newtonsoft deals with the conversion to int.

sheep
  • 31
  • 3
  • I do net get it as a string back from an API, but I can ask the person who programmed that part why he made that choice and maybe convince him to change that to a string. But then I still would not know what the property would be called (which is maybe also a problem?) – JeroenM May 17 '18 at 12:40
  • You really need to trust newtonsoft deserialisation on this. _But then I still would not know what the property would be called (which is maybe also a problem?)_ Which property are you refering ? 99 ? – sheep May 17 '18 at 13:00
  • _Object: an unordered collection of name–value pairs where the names (also called keys) are strings._ (cf. [link](https://en.wikipedia.org/wiki/JSON#Data_types,_syntax_and_example)) – sheep May 17 '18 at 13:07
0

I fixed it by doing the following:

var deserializedGetPaymentOptions = JsonConvert.DeserializeObject<Models.PaymentOptions>(await responseGetPaymentOptions.Content.ReadAsStringAsync());
                foreach (var desPayOp in deserializedGetPaymentOptions.PaymentMethods) {
                    Debug.WriteLine("start foreach");
                    foreach (KeyValuePair<int, string> issuerFromDict in desPayOp.Options.Issuers)
                    {
                        Debug.WriteLine(issuerFromDict.Key.ToString() + " : " + issuerFromDict.Value);
                    }
                }

I now deserialize it from PaymentOptions instead of PaymentMethods, then loop through the List and after that through the dictionary.

JeroenM
  • 807
  • 1
  • 11
  • 26