-1

My model :

public class RootObject
{
    public double Balance { get; set; }
    public string CardCode { get; set; }
    public string CardName { get; set; }
    public string PriceLevel { get; set; }
    public bool Status { get; set; }
}

My Json Response:

var response = "{\"C0001\":{\"Balance\":3.01,\"CardCode\":\"C0001\",\"CardName\":\"Mubarik\",\"PriceLevel\":\"PL1\",\"Status\":true}}"

How can I Deserialized this response to the following Model.

1
  • A good way to use JSON in C# is with JSON.NET Commented Jan 27, 2019 at 5:36

2 Answers 2

0

One of possible solutions is:

var result = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(response);
Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/a/54385322/7775908 is worth noticing because it's also working as expected.
-1

You can achieve it using Newtonsoft.Json.Linq.JObject like following code.

var response = "{\"C0001\":{\"Balance\":3.01,\"CardCode\":\"C0001\",\"CardName\":\"Mubarik\",\"PriceLevel\":\"PL1\",\"Status\":true}}";
JObject search = JObject.Parse(response);
RootObject yourObject = search["C0001"].ToObject<RootObject>();

Online Demo

Output

enter image description here

To read more about partial JSON fragment deserialization you can check here

EDIT:

what about this response? var bp = "{\"C0001\":{\"Balance\":3.01,\"CardCode\":\"C0001\",\"CardName\":\"Mubarik\",\"PriceLevel\":\"PL1\",\"Status\":true},\"C0002\":{\"Balance\":1.03,\"CardCode\":\"C0001\",\"CardName\":\"Richie Rich\",\"PriceLevel\":\"PL2\",\"Status\":true}}" – Mubah Mohamed

As per the format in the comment where you're getting multiple object in JSON with different ID, you can try like following to convert it into a list of RootObject.

 var response = "{\"C0001\":{\"Balance\":3.01,\"CardCode\":\"C0001\",\"CardName\":\"Mubarik\",\"PriceLevel\":\"PL1\",\"Status\":true},\"C0002\":{\"Balance\":1.03,\"CardCode\":\"C0001\",\"CardName\":\"Richie Rich\",\"PriceLevel\":\"PL2\",\"Status\":true}}";
 JObject search = JObject.Parse(response);
 IList<JToken> results = search.Children().ToList();
 List<RootObject> searchResults = new List<RootObject>();
 foreach (JToken result in results)
  {
    RootObject searchResult = result.First.ToObject<RootObject>();
    searchResults.Add(searchResult);
  }

Online Demo

14 Comments

Imho RootObject yourObject = search["C0001"].ToObject<RootObject>(); looks better. P.S. I'm not a downvoter
thanks @woldemar, you are right, I have updated the answer.
what about this response? var bp = "{\"C0001\":{\"Balance\":3.01,\"CardCode\":\"C0001\",\"CardName\":\"Mubarik\",\"PriceLevel\":\"PL1\",\"Status\":true},\"C0002\":{\"Balance\":1.03,\"CardCode\":\"C0001\",\"CardName\":\"Richie Rich\",\"PriceLevel\":\"PL2\",\"Status\":true}}"
In this case what do you want? do you want to get it as a list?
@MubahMohamed I have updated the answer, please check and let me know if it resolved your problem or not.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.