Convert JSON String To C# Object

前端 未结 14 2223
感情败类
感情败类 2020-11-22 14:27

Trying to convert a JSON string into an object in C#. Using a really simple test case:

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
obj         


        
14条回答
  •  佛祖请我去吃肉
    2020-11-22 14:49

    Here's a simple class I cobbled together from various posts.... It's been tested for about 15 minutes, but seems to work for my purposes. It uses JavascriptSerializer to do the work, which can be referenced in your app using the info detailed in this post.

    The below code can be run in LinqPad to test it out by:

    • Right clicking on your script tab in LinqPad, and choosing "Query Properties"
    • Referencing the "System.Web.Extensions.dll" in "Additional References"
    • Adding an "Additional Namespace Imports" of "System.Web.Script.Serialization".

    Hope it helps!

    void Main()
    {
      string json = @"
      {
        'glossary': 
        {
          'title': 'example glossary',
            'GlossDiv': 
            {
              'title': 'S',
              'GlossList': 
              {
                'GlossEntry': 
                {
                  'ID': 'SGML',
                  'ItemNumber': 2,          
                  'SortAs': 'SGML',
                  'GlossTerm': 'Standard Generalized Markup Language',
                  'Acronym': 'SGML',
                  'Abbrev': 'ISO 8879:1986',
                  'GlossDef': 
                  {
                    'para': 'A meta-markup language, used to create markup languages such as DocBook.',
                    'GlossSeeAlso': ['GML', 'XML']
                  },
                  'GlossSee': 'markup'
                }
              }
            }
        }
      }
    
      ";
    
      var d = new JsonDeserializer(json);
      d.GetString("glossary.title").Dump();
      d.GetString("glossary.GlossDiv.title").Dump();  
      d.GetString("glossary.GlossDiv.GlossList.GlossEntry.ID").Dump();  
      d.GetInt("glossary.GlossDiv.GlossList.GlossEntry.ItemNumber").Dump();    
      d.GetObject("glossary.GlossDiv.GlossList.GlossEntry.GlossDef").Dump();   
      d.GetObject("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso").Dump(); 
      d.GetObject("Some Path That Doesnt Exist.Or.Another").Dump();   
    }
    
    
    // Define other methods and classes here
    
    public class JsonDeserializer
    {
      private IDictionary jsonData { get; set; }
    
      public JsonDeserializer(string json)
      {
        var json_serializer = new JavaScriptSerializer();
    
        jsonData = (IDictionary)json_serializer.DeserializeObject(json);
      }
    
      public string GetString(string path)
      {
        return (string) GetObject(path);
      }
    
      public int? GetInt(string path)
      {
        int? result = null;
    
        object o = GetObject(path);
        if (o == null)
        {
          return result;
        }
    
        if (o is string)
        {
          result = Int32.Parse((string)o);
        }
        else
        {
          result = (Int32) o;
        }
    
        return result;
      }
    
      public object GetObject(string path)
      {
        object result = null;
    
        var curr = jsonData;
        var paths = path.Split('.');
        var pathCount = paths.Count();
    
        try
        {
          for (int i = 0; i < pathCount; i++)
          {
            var key = paths[i];
            if (i == (pathCount - 1))
            {
              result = curr[key];
            }
            else
            {
              curr = (IDictionary)curr[key];
            }
          }
        }
        catch
        {
          // Probably means an invalid path (ie object doesn't exist)
        }
    
        return result;
      }
    }
    

提交回复
热议问题