-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.

Alexander
  • 9,104
  • 1
  • 17
  • 41

2 Answers2

0

One of possible solutions is:

var result = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(response);
Alexander
  • 9,104
  • 1
  • 17
  • 41
-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

PSK
  • 17,547
  • 5
  • 32
  • 43
  • 1
    Imho `RootObject yourObject = search["C0001"].ToObject();` looks better. P.S. I'm not a downvoter – Woldemar89 Jan 27 '19 at 07:23
  • thanks @woldemar, you are right, I have updated the answer. – PSK Jan 27 '19 at 07:26
  • 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 Jan 27 '19 at 09:43
  • In this case what do you want? do you want to get it as a list? – PSK Jan 27 '19 at 09:53
  • @MubahMohamed I have updated the answer, please check and let me know if it resolved your problem or not. – PSK Jan 27 '19 at 10:03
  • still Not Working – Mubah Mohamed Jan 27 '19 at 10:24
  • check this online demo https://rextester.com/WWXJ24677 and let me know what is the issue? – PSK Jan 27 '19 at 10:27
  • I am Using C# on Xamarin – Mubah Mohamed Jan 27 '19 at 10:46
  • What error you are getting? I can’t guess the issue. You need to provide more detail about what is not working and what error you are getting – PSK Jan 27 '19 at 10:48
  • i am unable to return Deserialized Json Objects `public async Task> pullPartner() { string Url = "https://bloodbank-3fcd8.firebaseio.com/Partner.json"; HttpClient client = new HttpClient(); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, Url); HttpResponseMessage responseMessage = await client.SendAsync(requestMessage); var bp = await responseMessage.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject>(bp); }` Partner is RootObject – Mubah Mohamed Jan 27 '19 at 11:01
  • Its Not Working. – Mubah Mohamed Jan 27 '19 at 11:10
  • Its not working is not going to help, you should be telling what error/exception you are getting. How do I know what is the problem. – PSK Jan 27 '19 at 11:13
  • can i contact you via twitter – Mubah Mohamed Jan 27 '19 at 11:25
  • The Code above Works – Mubah Mohamed Jan 28 '19 at 03:42