IT TIP

C # 용 JSON 라이브러리

itqueen 2020. 10. 16. 19:20
반응형

C # 용 JSON 라이브러리


Microsoft는 C #에서 JSON을 사용할 수있는 라이브러리를 제공합니까? 그렇지 않은 경우 어떤 오픈 소스 라이브러리를 권장합니까?


JSON.Net


또한 내 ServiceStack JsonSerializer를 사용해보아야합니다. 이는 주요 JSON 직렬 변환기의 벤치 마크를 기반으로 현재 가장 빠른 .NET JSON 직렬 변환기 이며 모든 POCO 유형, DataContracts, 목록 / 사전, 인터페이스, 상속, 익명을 포함한 지연 바인딩 개체 직렬화 를 지원합니다. 유형 등

기본 예

var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = customer.ToJson();
var fromJson = json.FromJson<Customer>(); 

참고 : 다른 JSON 직렬 변환기보다 최대 40x-100x 더 느리기 때문에 벤치 마크에서 제외해야했기 때문에 성능이 중요하지 않은 경우에만 Microsoft의 JavaScriptSerializer를 사용하십시오 .


.net 프레임 워크는 JavaScriptSerializer를 통해 JSON을 지원합니다. 다음은 시작하기에 좋은 예입니다.

using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace GoogleTranslator.GoogleJSON
{
    public class FooTest
    {
        public void Test()
        {
            const string json = @"{
              ""DisplayFieldName"" : ""ObjectName"", 
              ""FieldAliases"" : {
                ""ObjectName"" : ""ObjectName"", 
                ""ObjectType"" : ""ObjectType""
              }, 
              ""PositionType"" : ""Point"", 
              ""Reference"" : {
                ""Id"" : 1111
              }, 
              ""Objects"" : [
                {
                  ""Attributes"" : {
                    ""ObjectName"" : ""test name"", 
                    ""ObjectType"" : ""test type""
                  }, 
                  ""Position"" : 
                  {
                    ""X"" : 5, 
                    ""Y"" : 7
                  }
                }
              ]
            }";

            var ser = new JavaScriptSerializer();
            ser.Deserialize<Foo>(json);
        }
    }

    public class Foo
    {
        public Foo() { Objects = new List<SubObject>(); }
        public string DisplayFieldName { get; set; }
        public NameTypePair FieldAliases { get; set; }
        public PositionType PositionType { get; set; }
        public Ref Reference { get; set; }
        public List<SubObject> Objects { get; set; }
    }

    public class NameTypePair
    {
        public string ObjectName { get; set; }
        public string ObjectType { get; set; }
    }

    public enum PositionType { None, Point }
    public class Ref
    {
        public int Id { get; set; }
    }

    public class SubObject
    {
        public NameTypePair Attributes { get; set; }
        public Position Position { get; set; }
    }

    public class Position
    {
        public int X { get; set; }
        public int Y { get; set; }
    }
}

If you look here, you will see several different libraries for JSON on C#.

http://json.org/

You will find a version for LINQ as well as some others. There are about 7 libraries for C# and JSON.


Is this what you're looking for?

System.Web.Script.Serialization.JavaScriptSerializer


Have a look at the system.web.script.serialization namespace (i think you will need .Net 3.5)


To give a more up to date answer to this question: yes, .Net includes JSON seriliazer/deserliazer since version 3.5 through the System.Runtime.Serialization.Json Namespace: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json(v=vs.110).aspx

But according to the creator of JSON.Net, the .Net Framework compared to his open source implementation is very much slower.


To answer your first question, Microsoft does ship a DataContractJsonSerializer: see msdn How to: Serialize and Deserialize JSON Data


Try the Vici Project, Vici Parser. It includes a JSON parser / tokeniser. It works great, we use it together with the MVC framework.

More info at: http://viciproject.com/wiki/projects/parser/home

I forgot to say that it is open source so you can always take a look at the code if you like.

참고URL : https://stackoverflow.com/questions/1474377/json-library-for-c-sharp

반응형