Neil P
Neil P

Reputation: 3190

How to address an object from a json array

Using the JSON library, I've imported an array of objects:

dynamic serviceList = JsonConvert.DeserializeObject(listString)

I've got an array of json objects, each has the property "name".

[
   {
      "name":"abcd",
      "properties":{
         "type":"1234"
      }
   },
   {
      "name":"xyz",
      "properties":{
         "type":"aaaa"
      }
   }
]

How do I address just the object "abcd"?

Upvotes: 0

Views: 171

Answers (5)

stuartd
stuartd

Reputation: 73243

This will get the value for you as a JObject, from which you can access the contained values:

var serviceList = JArray.Parse(listString);
var target = serviceList.Single(s => s["name"].ToString() == "abcd");
if (target != null)
{
    var type = target["properties"]["type"];
    // etc
}

Upvotes: 0

TommyN
TommyN

Reputation: 2381

You can access it like this:

  for (int i = 0; i < serviceList.Count; i++)
  {
      if (serviceList[i].name == "abc")
      {
         DoSomethingWith(serviceList[i];
         break;
      }
  }

Edit: didn't see that you wanted the "abc" element, so modified the code accordingly.

Upvotes: 0

Icepickle
Icepickle

Reputation: 12796

You can use the dynamic to do pretty much anything you would like it to do, so nothing would stop you from evaluating your result in the following way:

dynamic selectionList = JsonConvert.DeserializeObject( json );

foreach (var item in selectionList) {
    if ( string.Equals( (string)item.name, "abcd", StringComparison.OrdinalIgnoreCase ) ) {
        Console.WriteLine( item );
    }
}

This would work as per your original request, but I think you are making it a lot harder on yourself than need be :)

To see how this works, you could check this dotnetfiddle

I would probably create a class based on the spec, but I am assuming that properties is a dynamic list of properties and their values, meaning you would still end up with a Dictionary<string, object> in the end

Upvotes: 0

Prasad Telkikar
Prasad Telkikar

Reputation: 16049

You can parse your json Array using Newtonsoft.Json JArray.Parse() function.

Use FirstOrDefault() to get record where name is "abcd"

string listString = @"[{'name': 'abcd','properties': {'type': '1234'}},{'name': 'xyz', 'properties': { 'type': 'aaaa'}}]";
JArray jArray = JArray.Parse(listString);

//FirstOrDefault to get first record which satisfy the condition
var result = jArray.FirstOrDefault(x => (string)x["name"] == "abcd");
Console.WriteLine(result);

Output:

{
  "name": "abcd",
  "properties": {
    "type": "1234"
  }
}

.Net Fiddle

Upvotes: 1

Gianmarco Varriale
Gianmarco Varriale

Reputation: 163

The right way to solve the problem is create a static Object

Public Obj1 {

public string name {get;set;}
public Properties properties {get;set;}

}

Public Properties {

public string type {get;set;}

}

Then you can deserialize the JSON into a List<Obj1>, in this way you can iterate your list and find the "name":"abcd" Object. var myobj = mylist.FirstOrDefault(x=> x.name == "abcd")

Upvotes: 0

Related Questions