1

I have this JSON string called assignee:

{
    "id": 15247055788906,
    "gid": "15247055788906",
    "name": "Bo Sundahl",
    "resource_type": "user"
}

I want to get the "name" element and its value if it's not null. I have tried

var jobject = JsonConvert.DeserializeObject<JObject>(assignee);

And

var jo = JObject.Parse(assignee);

I tried looping through it but I just get null exception or empty output even though if I just print the assignee variable itself its filled with data.

My loop is like:

foreach (var result in jobject["name"])
{
    Debug.WriteLine(result);
}
5
  • 3
    Parse it to a proper C# class instead of JObject, it makes all this complication go away. Commented Mar 1, 2019 at 12:58
  • its really not necessary for such small data, string name = jobject["name"]; was enough Commented Mar 1, 2019 at 13:03
  • 1
    There are many benefits though. Compile time type checking is the obvious one. However there are less tangible ones, for example the readability of the code. Commented Mar 1, 2019 at 13:04
  • 1
    @fasola it's not about the verbosity of the method, it's about the correct way to handle it. That's why I deleted my answer and upvoted david's Commented Mar 1, 2019 at 13:04
  • Another point to note is that deserialising to a concrete class is almost twice as fast as using JObject. Commented Mar 1, 2019 at 13:20

5 Answers 5

3

The simplest and best way is to deserialise to a C# class, for example:

public class Data
{
    public long Id { get; set; }
    public string Name { get; set; }
    //etc..
}

And deserialise like this

var data = JsonConvert.DeserializeObject<Data>(json);
var name = data.Name;
Sign up to request clarification or add additional context in comments.

4 Comments

Upvoted...but, if the incoming data is "dirty" and you don't know all the forms, this approach can be problematic.
@Clay OP specifically needs the Name property, so that's not a concern here.
Agreed - just making a minor point. I do agree that this is the best approach...for both maintainability and performance reasons. I've experienced dirty data on larger json strings w/o knowing the source. Very unsatisfactory :-(
One work around for not knowing if a property exists or not is have a wrapper json package with meta data { "type" : "myType1", "Payload" : "{ inner json payload}" } This of course assumes you control the caller
1

To get name use this

string name = jobject["name"];

Comments

0

Using ["name"] returns a JToken, it is null if the property doesn't exist

JToken token = jo["name"];
Debug.WriteLine(token?.ToString() ?? "<default value>");

Comments

0

If you don't know properties beforehand, you can loop through JObject properties and get name value pairs as following:

var jsonObject = JObject.Parse(str);
foreach (var item in jsonObject)
{
   var name = item.Key;
   JToken token = item.Value;
   if (token is JValue)
   {
      var value = token.Value<string>();
   }
}

Comments

0

Here is how it should work:

class Data
  {
    public long? Id { get; set; }
    public string Gid { get; set; }
    public string Name { get; set; }
    public string Resource_Type { get; set; }
  }
  class Program
  {
    static void Main(string[] args)
    {

      string assignee = "{\"id\": 15247055788906, \"gid\": \"15247055788906\", \"name\": \"Bo Sundahl\", \"resource_type\": \"user\"}";
      Data data = JsonConvert.DeserializeObject<Data>(assignee);
      Console.WriteLine(data.Id);
      Console.WriteLine(data.Gid);
      Console.WriteLine(data.Name);
      Console.WriteLine(data.Resource_Type);
      Console.ReadLine();
    }
  }

Comments

Your Answer

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