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

12
  • 2
    Have you tried anything?? Commented Jun 4, 2018 at 6:27
  • tried to convering the following object to list but didnt work like this : var cars = ((IEnumerable)cars).Cast<object>().ToList() Commented Jun 4, 2018 at 6: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. Commented Jun 4, 2018 at 6:36
  • How are you parsing your json? Commented Jun 4, 2018 at 6: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. Commented Jun 4, 2018 at 6:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

He, you were faster ... gist-link example
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
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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.