0

We're trying to deserialize the following JSON string:

string responseData = "{\"status\":\"2000\",\"data\":[{\"response\":{\"TrxId\":4972783,\"result\":1,\"accruedPoints\":null}}],\"error\":null}";


var dic = JsonConvert.DeserializeObject<Dictionary<object, object>>(responseData);
object value, error, oData;

dic.TryGetValue("status", out value);
dic.TryGetValue("data", out oData);
string status = Convert.ToString(value);
string data = Convert.ToString(oData);
Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);

We're facing problems deserializing data. Following is the exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Is there a better way to deserialize this JSON object?
We need the accruedPoints value.

Filnor
  • 1,290
  • 2
  • 23
  • 28
Bridget
  • 183
  • 1
  • 7
  • 1
    Your JSON does not look directly dictionary compatible at first glance. Why should you cast it to Dictionary? – Amit Kumar Singh Oct 13 '17 at 06:25
  • So what do you suggest. How should this be done ? – Bridget Oct 13 '17 at 06:27
  • @Amit I tried using List. This is what I get: Unexpected character encountered while parsing value: {. Path '', line 2, position 3. – Bridget Oct 13 '17 at 06:30
  • Deserialize into a custom object with same field names. – Amit Kumar Singh Oct 13 '17 at 06:30
  • 1
    You need to first deserialize it to an array before trying to access the first object of the array. The value corresponding the the tag "data" is encapsulated in [ ], so it is an array not a JSONObject which is enclosed in { } – JustinDanielson Oct 13 '17 at 06:31
  • See the answer due to which this one is marked duplicate. You can copy this `{"status":"2000","data":[{"response":{"TrxId":4972783,"result":1,"accruedPoints":null}}],"error":null}` and paste as classes in visual studio and your classes will be created. I have formatted it correctly so that direct paste into visual studio will generate classes. – Amit Kumar Singh Oct 13 '17 at 06:31
  • When you generate classes, deserialize to Rootobject, and get accrued points as `Convert.ToString(root.data[0].response.accruedPoints);` – Amit Kumar Singh Oct 13 '17 at 06:34
  • Once your classes are generated, this code works. `string responseData = "{\"status\":\"2000\",\"data\":[{\"response\":{\"TrxId\":4972783,\"result\":1,\"accruedPoints\":null}}],\"error\":null}"; var root = JsonConvert.DeserializeObject(responseData); Console.WriteLine(root.data[0].response.accruedPoints); Console.ReadLine();` – Amit Kumar Singh Oct 13 '17 at 06:37
  • @amit Json2csharp couldn't generate the classes. – Bridget Oct 13 '17 at 06:42
  • You don't have visual studio? – Amit Kumar Singh Oct 13 '17 at 06:44
  • `public class Rootobject { public string status { get; set; } public Datum[] data { get; set; } public object error { get; set; } } public class Datum { public Response response { get; set; } } public class Response { public int TrxId { get; set; } public int result { get; set; } public object accruedPoints { get; set; } }` – Amit Kumar Singh Oct 13 '17 at 06:44
  • These are your classes. Just format it correctly. – Amit Kumar Singh Oct 13 '17 at 06:45

0 Answers0