-1

i've got a C# web Api receiving the following object :

     "cars": {
       "bmw": true,
       "benz": false,
       "kia": true,
       "hyundai": false,
       "madza": false,
       "ford": false
   }

the class property is as follows :

 public CarsViewModel cars{ get; set; }

How can i get all the values that are true in the above object?

Silva
  • 405
  • 1
  • 3
  • 13
  • 2
    Have you tried anything?? – Ipsit Gaur Jun 04 '18 at 06:27
  • tried to convering the following object to list but didnt work like this : `var cars = ((IEnumerable)cars).Cast().ToList()` – Silva Jun 04 '18 at 06:30
  • 1
    Your JSON isn't returning any form of IEnumerable (array / list / etc). It is returning a single object with a bunch of bool properties. – Adam G Jun 04 '18 at 06:36
  • How are you parsing your json? – Gaurang Dave Jun 04 '18 at 06:38
  • correct, i didnt want to go the long way of check each value if its true, i thought the is a better short way to handle it, maybe in the future i decide to add an extra car name. – Silva Jun 04 '18 at 06:40
  • " maybe in the future i decide to add an extra car name.", If you have any power on this json please send a list of true so you don't have to handle the false. and if all possible choice are needed, send a list of True and a list of All. this will be easier for every one. – Drag and Drop Jun 04 '18 at 06:44
  • the Json data is converted on the model state. – Silva Jun 04 '18 at 06:44
  • Show us your CarsViewModel class. – 1_bug Jun 04 '18 at 06:51
  • the class is as follows : `public class CarsViewModel { public bool bmw { get; set; } public bool benz { get; set; } public bool kia { get; set; } public bool hyundai { get; set; } public bool mazda { get; set; } public bool ford { get; set; } }` – Silva Jun 04 '18 at 06:54
  • And what's your desired output? Just strings: `bmw`, `kia`? – 1_bug Jun 04 '18 at 07:03
  • yes, just a string or list of all values that are true – Silva Jun 04 '18 at 07:06
  • Your best option is to convert your car model like the following and then easily filter it : public List> cars {get;set;} – Utkarsh Bais Jun 04 '18 at 07:12

2 Answers2

2

You can parse the received object into the dictionary and select the key only if value is true.

string json = "{\"bmw\": true,\"benz\": false,\"kia\": true,\"hyundai\": false,\"madza\": false,\"ford\": false}";
var dict = JsonConvert.DeserializeObject<Dictionary<string,bool>>(json);

List<string> cars = dict.Where(x=>x.Value).Select(y=>y.Key).ToList();

You can check the result by:

cars.ForEach(y => Console.Write("{0}\n", y));

PS. For serializing you have to use Newtonsoft.Json namespace.

1_bug
  • 4,454
  • 3
  • 39
  • 49
  • 1
    He, you were faster ... [gist-link example](https://gist.github.com/nkreter/f743e46b9a130d3401938d386fd7cdb1) – nilsK Jun 04 '18 at 07:14
  • Thanks, had to `JsonConvert.SerializeObject(cars)` then `DeserializeObject`. works perfect but dont know if `SerializeObject` and `DeserializeObject` the same object is good code practices but thank you very much – Silva Jun 04 '18 at 07:39
0

you try this code

 string jsonstring = "{\"cars\": {\"bmw\": \"true\", \"benz\": \"false\",  \"kia\": \"true\",  \"hyundai\": \"false\", \"madza\": \"false\",  \"ford\": \"false\"}}";
 dynamic data = JObject.Parse(jsonstring); 
 CarsViewModel obj = new CarsViewModel(); 
 var mydetails2 = JsonConvert.SerializeObject(data.cars);
 var mydetails3 = JsonConvert.DeserializeObject<Dictionary<string, bool>>(mydetails2);

  foreach (var pair in mydetails3)
    {
       // Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
        if (pair.Value == true)
        {
            if (pair.Key == "bmw")
            {
                obj.bmw = pair.Value;
            }
            else if (pair.Key == "benz")
            {
                obj.benz = pair.Value;
            }
            else if (pair.Key == "kia")
            {
                obj.kia = pair.Value;
            }
            else if (pair.Key == "hyundai")
            {
                obj.hyundai = pair.Value;
            }
            else if (pair.Key == "madza")
            {
                obj.madza = pair.Value;
            }
            else if (pair.Key == "ford")
            {
                obj.ford = pair.Value;
            }

        }
    }

I hope its helpful to you..

Angappan.S
  • 90
  • 8