Hi all here comes a noob question,
i am trying around with twitter library "linq2twitter" in C#.
I would like to get the content from :
System.Collections.Generic.List <.LinqToTwitter.MediaEntity.>
My attempt is simple:
public string[] MediaContent;
MediaContent = tweet.Entities.MediaEntities.ToArray()
I tried to convert it into an string[] array with the .ToArray() function but i am getting an the error "conversion from Type LinqToTwitter.MediaEntity[] in string[] is not possible"
I guess there is a better way to get the data from this Type of List ?
Can someone give me a tip?
thx,
Fid
You could use the technique in thorkia's comment to obtain one of the string properties of MediaEntity, but it's not clear that's what you want. Here's how to convert the List to a MediaEntity[]:
public MediaEntity[] MediaContent;
MediaContent = tweet.Entities.MediaEntities.ToArray()
Here, all I did was change the array type from string[] to MediaEntity[].
Related
Thanks for someone who might be able to help me further, as I can't find an answer in the www right now. I actually program VB.net, but since this language is now dissolved and not further developed, I want to switch to C#. I am getting on well, because a lot of things are similar. There is only one method where I am stuck.
In vb.net I always liked to use the method:
mystring.split(seperator)(part)
to get a certain string from a CSV line and work with it. How does this work in C#? I know that I can load everything into an array, but I don't want to do that in this case. A simple split and the number of the element is enough. Can someone help me?
Seems like the same split method is what you need? It returns an array which you could store or just access directly. The difference being in C# you use [index] instead. So something like this:
string myFile = File.ReadAllText("myfile.txt");
string firstLine = myFile.Split("\r\n")[0];
I have a long json string that I get from reddit which looks like: this. Once I download and parse the string, I am looking for two parts, "fallback_url" and "thumbnail". The thing is that depending on the time that the reddit post was uploaded the format of the json string can vary so those parts can be in different spots. What I want to do is look through the objects and arrays in the JArray to find the value of "fallback_url" and "thumbnail".
I am trying to use Newtonsoft, but I am not very familiar with it, and when I try the following I get an error:
var data = JArray.Parse(json);
string path = data.Value<string>("fallback_url")?? data.Value<string>("url_overridden_by_dest");
System.ArgumentException: Accessed JArray values with invalid key value: "fallback_url". Int32 array index expected.
Clearly, I'm either using the function wrong or it's not the right function to use. I've tried looking online for answers, but all the examples I found directly referenced the structure of the json to find a value from a key, which I can't do.
I have a string variable like so, which I am receiving from a third party API:
string strArray = "[[1,\"56353657\",\"300\",\"test\",\"<img src=\\\"../Images/Edit.gif\\\" id=\\\"Edit\\\" />\",\"<img src=\\\"../Images/Delete.gif\\\" id=\\\"Delete\\\" />\"],[2,\"56353657\",\"400\",\"test\",\"<img src=\\\"../Images/Edit.gif\\\" id=\\\"Edit\\\" />\",\"<img src=\\\"../Images/Delete.gif\\\" id=\\\"Delete\\\" />\"]]";
I would like to be able to loop through this and retrieve the first 3 items in each of the arrays.
Could someone please advise me as to how to achieve this using c#?
Because values in your array of arrays are not of the same type you can try this way:
String[][] table = JsonConvert.DeserializeObject<String[][]>(strArray);
Then you can loop through this and, if you need, convert values to the desired type.
I am working on Marketing APIs. I want to hit this Api to get Estimate of Target Audiences, basically how much users will be able to see my ad.
"https://graph.facebook.com/v2.2/{adaccountid}/reachestimate"
I am getting the response until I pass this parameter "flexible_spec". Whenever I pass this parameter I get this response "The type string is expected but a type integer was received with value 0". I searched for it but no solution was there.
I will show you what I tried until now:
1)"https://graph.facebook.com/v2.2//{adaccountid}/reachestimate? targeting_spec={"geo_locations":{"countries": ["IN"]},"flexible_spec":{"interests":["6006289279425"]}}
2) "https://graph.facebook.com/v2.2//{adaccountid}/reachestimate? targeting_spec={"geo_locations":{"countries": ["IN"]},"flexible_spec":{"interests":["Movies"]}}
3)"https://graph.facebook.com/v2.2//{adaccountid}/reachestimate? targeting_spec={"geo_locations":{"countries": ["IN"]},"flexible_spec":{"interests":["id":"6006289279425","name":"Movies"]}}
I have tried sending an array of ids in flexible spec interests , array of name's in flexible_spec interests and I have also tried sending list<interests> in it. But no luck.
I searched alot about this error. Then i came to know about my mistake i am doing in this API. We can specify interests fields in two types:
1) targeting_spec={"geo_locations":{"countries":"IN"]},"flexible_spec":[{"interests":["6006289279425"]}]}
2) targeting_spec={"geo_locations":{"countries": ["IN"]},"interests":["6006289279425","46345343534"]}
first one passing interests array in flexible_spec array.
second one passing interests as an array object but not in flexible_spec.
This helped me . I am posting this answer because it might help you too in the same situation.
Thanks.
This question already has answers here:
How to cast System.Object[*] to System.Object[]
(3 answers)
Closed 8 years ago.
I have a COM that reads our VMS system and returns an array for Dynamic type.
In visual studio when I inspect the type it shows:
dynamic {object[]}
so I tried the following:
var lines = myApp.GetScreenLines();
foreach(var line in lines)
{
//Do something
}
But I keep getting the exception:
Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.
However, if I inspect lines, it shows just like a normal array and I can even expand it to see the data.
Does anyone know how I can convert this dynamic array data type to a string type?
I have tried the solutions in the duplicate link yet they dont seem to work as they assume I know information about the array. But as its dynamic I dont know anything until runtime, so i cant get the lower or upper bounds of the array.
PS I have no control over what myApp.GetScreenLines(); returns as its a COM API.
EDIT:
The solutions provided in the duplicate do not resolve my issue. I have tried both.
The first one ive tried was:
Array sourceArray = myWinFormsApp.GetScreenLines();
if (sourceArray.Rank != 1)
throw new InvalidOperationException("Expected a single-rank array.");
object[] newArray2 = new object[sourceArray.Length];
Array.Copy(sourceArray, sourceArray.GetLowerBound(0),
newArray2, 0, sourceArray.Length);
This results in the exception: Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.
The second one I tried was:
object[] newArray = myWinFormsApp.GetScreenLines().Cast<object>().ToArray();
To which I get the exception: 'System.Array' does not contain a definition for 'Cast'
So I dont consider this question a duplicate, as ive explained I can view the array using the "watch" tool in visual studio, but I cant seem to iterate through it
Try using dynamic instead of var
var lines = myApp.GetScreenLines();
foreach(dynamic line in lines)
{
//Do something
}