dotnet 객체를 json에 json으로 직렬화합니다. 여기서 key는 객체의 속성이고 전체 객체의 값입니다.

FDB

이 json을 정확하게 만들어야합니다. 문자열 조작을 사용할 수 있지만 클래스를 사용하여 만들고 싶습니다.

{  
    "articles":{  
        "3":{  
            "id":"3",
            "label":"Preambolo",
            "leaf":true,
            "pathArray":[  
                {  
                    "id":"1",
                    "label":"TITOLO I Disposizioni Generali"
                },
                {  
                    "id":"2",
                    "label":"Capo I Definizioni e classificazioni generali"
                }
            ],
            "path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
        }
    }
}

이것은 내 코드입니다.

public class PathArrayItem {
    public string id {
        get;
        set;
    }
    public string label {
        get;
        set;
    }

    public PathArrayItem(string id, string label) {
        this.id = id;
        this.label = label;
    }
}

public class item {
    public string id {
        get;
        set;
    }
    public string label {
        get;
        set;
    }
    public bool leaf {
        get;
        set;
    }
    public List < PathArrayItem > pathArray {
        get;
        set;
    }
    public string path {
        get;
        set;
    }
}

void test() {
    List < item > art = new List < item > ();
    item item_3 = new item();
    item_3.id = "3";
    item_3.label = "Preambolo";
    item_3.leaf = true;
    item_3.pathArray = new List < PathArrayItem > ();
    item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
    item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
    item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";
    art.Add(item_3);

    txtOutput.Text = Newtonsoft.Json.JsonConvert.SerializeObject(new {
        articles = art
    });
}

이것은 내 코드가 생성하는 출력입니다.

{  
    "articles":[  
        {  
            "id":"3",
            "label":"Preambolo",
            "leaf":true,
            "pathArray":[  
                {  
                    "id":"1",
                    "label":"TITOLO I Disposizioni Generali"
                },
                {  
                    "id":"2",
                    "label":"Capo I Definizioni e classificazioni generali"
                }
            ],
            "path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
        }
    ]
}

변경해야 할 사항 :

  • 기사는 배열이 아니어야합니다.
  • 각 항목은 id가 키이고 전체 객체가 값인 jproperty로 직렬화되어야합니다.
남자 이름

사용하는 대신 List<item>사용할 수 있습니다Dictionary<int, item>

Dictionary<int, item> art = new Dictionary<int, item>();
item item_3 = new item();
item_3.id = "3";
item_3.label = "Preambolo";
item_3.leaf = true;
item_3.pathArray = new List<PathArrayItem>();
item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";

art.Add(3, item_3);

당신에게 줄 것입니다

{"articles":{"3":{"id":"3","label":"Preambolo","leaf":true,"pathArray":[{"id":"1","label":"TITOLO I Disposizioni Generali"},{"id":"2","label":"Capo I Definizioni e classificazioni generali"}],"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"}}}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관