ASP.NET Newtonsoft.Json JObject.Parse And JsonConverter.DeserializeObject

2024-04-02

比較 C# 使用 Newtonsoft.Json 處理 JSON 資料時 JsonConvert.DeserializeObject()JObject.Parse() 兩種方法的主要差異。

logo

說明

JsonConvert.DeserializeObject JObject.Parse()
Type Generic T of C# class JObject to represent JSON Object
Usage JSON String to C# class JSON String to JObject class
Flex 較低,需要預先定義 C# class 可以在 不知道 Json 格式下處理 Json 資料
Performance Better Slower
Scenario 所處理的 JSON 格式固定且需要強型別的操作時 當 JSON 格式較為複雜、動態或僅是拋棄式的使用情境

如果你的應用場景中 JSON 結構是預先知道的,並且你希望利用 C# 的類型系統進行強類型操作,則 JsonConvert.DeserializeObject() 是一個好選擇;相反,如果你需要更多的靈活性來處理各種不確定的 JSON 結構,或者需要在運行時探索和操作 JSON 數據,則 JObject.Parse() 更為合適。

Sample Code

{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "Anystate"
  },
  "phoneNumbers": [
    {"type": "home", "number": "555-1234"},
    {"type": "work", "number": "555-5678"}
  ]
}

JObject.Parse()

var name = jObject["name"].ToString();
var age = (int)jObject["age"];

// Check if a key exists
if (jObject.ContainsKey("address"))
{
    // Access nested object
    var street = jObject["address"]["street"].ToString();
    var city = jObject["address"]["city"].ToString();
    var state = jObject["address"]["state"].ToString();
}

// Check if an array exists and access its elements
if (jObject.ContainsKey("phoneNumbers") && jObject["phoneNumbers"].Type == JTokenType.Array)
{
    foreach (var phone in jObject["phoneNumbers"])
    {
        var type = phone["type"].ToString();
        var number = phone["number"].ToString();
        // Process each phone number here
    }
}

JsonConvert.DeserializeObject

using Newtonsoft.Json;
using System.Collections.Generic;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Address Address { get; set; }
    public List<PhoneNumber> PhoneNumbers { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class PhoneNumber
{
    public string Type { get; set; }
    public string Number { get; set; }
}

// settings is optional
var settings = new JsonSerializerSettings
{
    // Avoid converting dates to local times (if not needed)
    DateParseHandling = DateParseHandling.DateTimeOffset,
    // Minimize memory usage by not storing additional metadata
    MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
    // Use the fastest available method to parse JSON
    // This can make a big difference for large JSON payloads
    MaxDepth = 64, // Increase or decrease based on your JSON structure's depth
};

var person = JsonConvert.DeserializeObject<Person>(jsonString, settings);