0

Sorry for basic question, I'm beginner and tried to find my problem, but wasn't able to

string json = "{\"EmailList\":[{\"name\":\"John Bravo\",\"email\":\"[email protected]\"},{\"name\":\"Daniel Alutcher\",\"email\":\"[email protected]\"},{\"name\":\"James Rodriguez\",\"email\":\"[email protected]\"}]}";

JObject rss = JObject.Parse(json);

var data = JsonConvert.DeserializeObject<dynamic>(json);

dynamic emails = data.EmailList;

List<string> emailList = new List<string>();

foreach (dynamic item in emails)
{
    int x = 0;
    if (item.email != null)
    {
        Console.WriteLine(item.email);
        //emailList.Add(item.email); // throws exception System.Collections.Generic.List<string>.Add(string)' has some invalid arguments' 
    }
}

So I'm looping trough this JSON and I'm able to get in console each email, but when I'm trying to add it to a list, it's throwing exception error

3
  • list.Add(item.email as string); Have fun :) Commented Oct 22, 2020 at 13:55
  • @Leron_says_get_back_Monica While that will get rid of the exception, the list will be full of nulls, since the dynamic objects aren't strings as such. Commented Oct 22, 2020 at 14:06
  • You can simplify this whole question down to this code: dynamic test = new JValue("Test"); new List<string>().Add(test);, and I wonder why that fails, since string s = test; works just fine. Commented Oct 22, 2020 at 14:08

2 Answers 2

1

As you commented "IS there any way to avoid "dynamic" in this code?".

Well there is and it's pretty simple, It's a simple copy past!
In Visual Studio using the special past in top bar menu (image).
Or in an online tool like app.quicktype.io or json2csharp.com.

using  Newtonsoft.Json;

public partial class JsonData //Give it a better name
{
       [JsonProperty("EmailList")] public List<EmailInformation> EmailList { get; set; }
}

public partial class EmailInformation
{
       [JsonProperty("name")]  public string Name { get; set; } 
       [JsonProperty("email")] public string Email { get; set; }
}

And the usage is pretty straightforward too, and you already have most of it in your code:

var data = JsonConvert.DeserializeObject<JsonData>(json);

foreach(var mailInfo  in data.EmailList){
       Console.WriteLine($"{mailInfo.Name} <{mailInfo.Email}>;");
}

//here a list of string 
var emails = data.EmailList.Select(x=> x.Email).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! This is exactly what I needed :)
and what if this JSON would be in Appsettings as a part of whole appsettings json? How would be it possible to import it to Program.cs ?
@onlyo, you are on Core? How to read AppSettings values from a .json file. Then The documentation will be the thing to check learn.microsoft.com/en-us/aspnet/core/fundamentals/…. Hierachical and Bind an array will be a must read.
0

You're Trying to add dynamic in List<string> instead you should do

emailList.Add(item.email.ToString()); 

Code:

string json =
"{\"EmailList\":[{\"name\":\"John Bravo\",\"email\":\"[email protected]\"},{\"name\":\"Daniel Alutcher\",\"email\":\"[email protected]\"},{\"name\":\"James Rodriguez\",\"email\":\"[email protected]\"}]}";


JObject rss = JObject.Parse(json);

var data = JsonConvert.DeserializeObject<dynamic>(json);

dynamic emails = data.EmailList;

List<string> emailList = new List<string>();

foreach (dynamic item in emails)
{
    int x = 0;
    if (item.email != null)
    {
        Console.WriteLine(item.email);
        emailList.Add(item.email.ToString()); // Change here 
   }
}

11 Comments

I am wondering why .Add((string)item.email) works just fine, but .Add(item.email) doesn't.
@LasseV.Karlsen IMO item.email in the current state has type dynamic which is not string what Add requires. so we explicitly need to cast that.
Adding dynamic to a List<string> should really work just fine, assuming the value in the dynamic is coercible to a string. For instance, you can do this, and it works: string s = item.email; The underlying object is of type JValue, but again, it works when assigning directly to a string variable, so I wonder why it doesn't allow this when calling a method that only accepts a string parameter.
I asked a new question instead - stackoverflow.com/questions/64484213/…
@LasseV.Karlsen I didn't see your edited comment about JValue, now I'm as much curious as you're, following your asked question. thanks for asking this.
|

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.