[{"conversation":{"id":"04d27d987de7f897580096b099815691cd4a89_ecf47fb8-cd72-4e5d-925c-5a63aa2fb315","wid":"04d27d987de7f897580096b099815691cd4a89","nicknames":{"owner":"Wiz_boltebony","originator":"Username123"},"group_token":"5a4b2b9d-ed39-4029-a76e-347a8c99806b"}},{"conversation":{"id":"05043a6393ec32806194414f2239a8697fa788_ecf47fb8-cd72-4e5d-925c-5a63aa2fb315","wid":"05043a6393ec32806194414f2239a8697fa788","nicknames":{"owner":"Summer_Reflection","originator":"Wiz_boltebony"},"group_token":"0b77eb02-aa57-4811-91fd-5fa61997b6a0"}}]
I want to parse out all of the (group_token":"0b77eb02-aa)...etc values from this json "array".
Here is my code:
dynamic j = JsonConvert.DeserializeObject(contents);
foreach (var c in j[0]["conversation"])
{
Console.WriteLine(c["group_token"]);
}
Here is an image on how the JSON is laid out:
http://gyazo.com/5840a31b71d4cbea626899030debe5d8
My code doesn't work at all! How do I go about extracting these group_token values?
You need to change your code a little bit (iterate through objects, not properties);
dynamic j = JsonConvert.DeserializeObject(contents);
foreach (var c in j)
Console.WriteLine(c["conversation"]["group_token"]);
Your current code iterates through properties of first conversation object and tries to get group_token child of each property, which is wrong.
You can try below as well. Totally agreed with #Ulugbek Umirov
var _jArr = (JArray)JsonConvert.DeserializeObject(contents);
IEnumerable<string> _groupToken = _jArr.Select(conv => conv["conversation"]["group_token"].ToString());
Related
So I need to calculate how many fields in a JObject has a value that is not null or whitespace. I came up with the solution below that works fine. However, I am wondering if there is a way to do it with a fancy one-liner in LINQ instead?
JObject jObject = JObject.Parse(#"{
""Name"":""Nisse"" ,
""Address"":""Road1"",
""Zip"":"""",
}");
var counter = 0;
foreach (var x in jObject)
{
if (!string.IsNullOrWhiteSpace(x.Value.ToString()))
counter++;
}
//Counter is now 2
Below check each property and return you not empty properties
jObject.Children().AsEnumerable().ToList()
.Count(t=> !string.IsNullOrWhiteSpace( t.Values().ToList()[0].ToString()))
Note : this code is based on the object structure provided in question.
You could use int counter = jObject.Children().Count(c => string.IsNullOrEmpty(((JProperty)c).Value.ToString()));, but see for yourself if that is more readable.
I have a list of strings List<string> blobsToCopy
var newList = blobsToCopy.Select(GetDiskNameWithoutFolder);
private static string GetDiskNameWithoutFolder(string path) {...}
How to change all values in the blobsToCopy without creating a new IEnumerable<string>?
You don't use LINQ, just use a simple for loop and replace each item with its modified version.
for (var i = 0; i < blobsToCopy.Count; i++)
{
blobsToCopy[i] = GetDiskNameWithoutFolder(blobsToCopy[i]);
}
Unless I'm missing something obvious, what's wrong with a good old-fashion for loop?
for(var i=0; i < blobsToCopy.Count;i++)
{
// do whatever....
}
Well, strings are immutable, so you can't edit those directly. Instead you could edit your list with blobsToCopy[i]. This will keep your list intact and let you edit which string the element of the list points to.
Maybe you could use the ForEach linq implementation. Don't have a c# compiler available at the moment, so I can't test this.
list.ForEach(c => c = GetDiskNameWithoutFolder(c));
I have a C# list of objects and I'm trying to access its nested object. Here's the structure from Visual Studio's debugger (sorry I can't embed images since I'm a newbie to the site):
>product.Product {Product.AxdEntity_Product_EcoResProduct[1]}
>> - [0] {Product.EcoResProductMaster} // #1 - Please note curley braces
>>> - [Product.EcoResProductMaster] // #2 - Please note brackes
>>> + base {Product....
>>> + ModelingPolicy
>> + Identifier
To access the properties in #1, I would do the following:
var prod = product.Product[0];
Then I can access "Identifier" as such:
var identifier = prod.Identifier[0]...
To access the properties in #2 (such as ModelingPolicy), I'm not sure how to go about it:
var prod = product.Product[0][WhatShouldGoHere?].ModelingPolicy[0] ...?? I need help here
Eventually, I'd like to access the ModelingPolicy[0] like I did with prod.Identifier[0].
The Product class is being returned from a web service and I don't have access to its definition. At least I don't think I do.
Thank you for any guidance!
Looks to me like:
var ecoResProductMaster = product.Product[0].EcoResProductMaster; // Product type
var modelingPolicy = ecoResProductMaster.ModelingPolicy;
Have you tried this?
var productList = product.ToList()
foreach(var prod in productList)
{
var identifier = prod.identifier
foreach (var modelingPolicy in identifier.ModelingPolicy)
{
//do somthing with modeling policy
}
//do more stuff with product
}
As an aside please sort your naming conventions out. Having an array called product that contains a number of Product objects is just confusing
It's difficult to tell what you're going for here. Is this a List or array?
If it is a list then you can just foreach through the list to access each object.
Like if you have
List<SomeObject> someObjectList = new List<SomeObject>();
Then after you add a bunch of SomeObjects to the List you can just access the SomeObjects in a foreach loop.
foreach (SomeObject s in someObjectList)
{
// then to access nested objects
var n = s.SomeNestedObject;
// do something with n
}
Thanks all for your input. The answer from SmartDev helped me access the objects in the inner array. To do this, simply do the following:
var productMaster = (EcoResProductMaster)product.Product[0];
var modelingPolicy = productMaster.ModelingPolicy[0];
Of course, this is assuming that only one object is returned in the array, hence "[0]" but this should eventually go inside a foreach loop.
What I learned was if an object is returned without an index such as //#2 as noted above, a type cast should allow me to access it. However, if an index is returned such as //#1, then using [0] will allow me to access its properties.
I want to retrieve hierarchy from one of the cube. I want to form a JSON structure so I am hoping if I can use ADOMD and use a recursive function to get this information and show the result in TreePanel.
I need to form JSON from the output.
foreach (var att in dimension.Hierarchies)
{
foreach (var m in att.Levels[1].GetMembers())
{
var path = att.UniqueName;
}
}
The above code only gets me level 1 attributes. I don't know how to get all the child attributes for given attribute.
Please help
To modify your original code to loop all the levels (instead of just level 1) is simple, but I'm guessing you are after the names of the members within each level.
Your original line var path = att.UniqueName; will return the same value many times over, won't it?
foreach (var att in dimension.Hierarchies)
{
foreach (var lev in att.Levels) //NEW LOOP
{
foreach (var m in lev.GetMembers())
{
var membername = m.UniqueName; //GET VALUE HERE
}
}
}
Where I have used UniqueName you could use any member property - read about ADOMD to find out what is available.
I am wanting to trim any white space off a collection of strings. I used the following code but it doesn't seem to work. Could anyone explain why?
result.ForEach(f => f = f.Trim());
This won't work because you are assigning a new string reference to a local variable. This is probably what you are looking for:
result = result.Select(f => f.Trim()).ToList();
You are re-assigning the argument variable inside the scope of the lambda. It's a collapsed form of:
foreach(string value in myList)
{
Lambda(value);
}
void Lambda(string input)
{
input = input.Trim();
}
The simplest way would probably be to use a projection:
myList = myList.Select(str => str.Trim()).ToList();
foreach doesn't give you write access to the underlying collection, it only iterates through it, which means your change isn't stored back into the collection.
You can do two things:
Produce a new collection
var newResult = result.Select(f => f.Trim()).ToList();
Use a normal for-loop and change the original collection
for (int index = 0; index < result.Count; index++)
result[index] = result[index].Trim();