JSON Data key Ordering

Source: Internet
Author: User

 // <summary>       ///JSON formatted reordering/// </summary>       /// <param name= "Jobj" >Raw JSON Jtoken.parse (string json);</param>       /// <param name= "obj" >Initial value NULL</param>       /// <returns></returns>        Public Static stringSortjson (Jtoken jobj, Jtoken obj) {if(obj = =NULL) {obj=NewJobject (); } List<JToken> list = Jobj. Tolist<jtoken>(); if(Jobj. Type = = Jtokentype.object)//Non-array{List<string> Listsort =Newlist<string>(); foreach(varIteminchlist) {                   stringName=jproperty.load (item. Createreader ()).                   Name; Listsort.                ADD (name); } listsort.                Sort (); List<JToken> listtemp =NewList<jtoken>(); foreach(varIteminchListsort) {Listtemp.add (list. Where (P=>jproperty.load (P.createreader ()). name==item).                FirstOrDefault ()); } list=listtemp;                foreach(varIteminchlist) {Jproperty JP=jproperty.load (item.                    Createreader ()); if(item. First.type = =jtokentype.object) {Jobject sub=NewJobject (); (obj asjobject). ADD (JP.                        Name, sub); Sortjson (item.                    First, sub); }                    Else if(item. First.type = =jtokentype.array) {Jarray arr=NewJarray (); if(obj. Type = =jtokentype.object) {(obj asjobject). ADD (JP.                        Name, arr); }                        Else if(obj. Type = =jtokentype.array) {(obj asJarray).                        ADD (arr); } Sortjson (item.                    First, arr); }                    Else if(item. First.type! = Jtokentype.object && item. First.type! =jtokentype.array) {(obj asjobject). ADD (JP. Name, item.                    First); }                }            }            Else if(Jobj. Type = = Jtokentype.array)//Array            {                foreach(varIteminchlist) {List<JToken> Listtoken = Item. Tolist<jtoken>(); List<string> Listsort =Newlist<string>(); foreach(varIminchListtoken) {                        if(IM. Type = =jtokentype.object) {Jobject sub=NewJobject (); (obj asJarray).                                ADD (sub);                        Sortjson (IM, sub); }                        Else {                            stringName =jproperty.load (im. Createreader ()).                            Name; Listsort.                        ADD (name); }} listsort.                    Sort (); List<JToken> listtemp =NewList<jtoken>(); foreach(varIm2inchListsort) {Listtemp.add (Listtoken.where (P= Jproperty.load (P.createreader ()). Name = =im2).                    FirstOrDefault ()); } list=listtemp; Listtoken=list;Jobject item_obj =NewJobject (); foreach(varTokeninchListtoken) {Jproperty JP=jproperty.load (token.                        Createreader ()); if(token.) First.type = =jtokentype.object) {Jobject sub=NewJobject (); (obj asjobject). ADD (JP.                            Name, sub); Sortjson (token.                        First, sub); }                        Else if(token.) First.type = =jtokentype.array) {Jarray arr=NewJarray (); if(obj. Type = =jtokentype.object) {(obj asjobject). ADD (JP.                            Name, arr); }                            Else if(obj. Type = =jtokentype.array) {(obj asJarray).                            ADD (arr); } sortjson (token.                        First, arr); }                        Else if(item. First.type! = Jtokentype.object && item. First.type! =Jtokentype.array) {if(obj. Type = =jtokentype.object) {(obj asjobject). ADD (JP. Name,token.                            First); }                            Else if(obj. Type = =jtokentype.array) {item_obj. ADD (JP. Name, token.                            First); }                        }                    }                    if(obj. Type = =jtokentype.array) {(obj asJarray).                    ADD (Item_obj); }                }            }            stringRET =obj.            ToString (formatting.indented); returnret; }

JSON Data key Ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.