(Multi-object) JSON converted to list

Source: Internet
Author: User

The writing is not good, please forgive us.

1.Json format
{"Packages": [{"Type": "AAA"}], "Zone_packages": [{"TicketType": [{"Ticket_type": "111", "Ticket_count": "1", "Trans_ Num ": null," Refund_num ": null}]," Zone_group ":" Wuhan Garden Expo "," Ticket_key ":" 1753626040 "}]," ORDER_NBR ":" 30000000012015090800006524 "," Order_amount ":" "," order_nbr_original ":" 150908050106992 "," order_contacts ":" 仲秀萍 "," Contacts_tel ":" 15821311320 "," Order_remark ":" "," Use_date ":" 20150909 "," Order_status ":" 8 "}

2. Define extension methods
//<summary> split string becomes a name value dictionary </summary>
       // <param name= "str" > </param> to split the string;
       //<param name= " Namevalueseparator "> key-value pair delimiter </param>
       //<param name=" Separators "> Split character </param>
       //<returns> key-value pairs dictionary </ Returns>
        public static idictionary<string, string> Splitasdictionary (This string str, string namevalueseparator = "=", params string[] separators)
   & nbsp;    {
            var dic = new Dictionary<string, string> ();
            if (string.isnullorwhitespace (str)) return DIC;

if (String.IsNullOrEmpty (namevalueseparator)) Namevalueseparator = "=";
if (separators = = NULL | | separators. Length < 1) separators = new string[] {",", ";"};

string[] ss = str. Split (separators, stringsplitoptions.removeemptyentries);
if (ss = = NULL | | ss. Length < 1) return null;

foreach (var item in SS)
{
Int32 p = Item. IndexOf (Namevalueseparator);
Not before or after.
if (P <= 0 | | p >= item. LENGTH-1) continue;

String key = Item. Substring (0, p). Trim ();
Dic[key] = Item. Substring (P + namevalueseparator.length). Trim ();
}

return dic;
}

3.Json Format parsing
public static Object Zoneticket (String json)
{
var doc = json;
JSON format parsing
var diclist = doc. Split (new string[] {"\" zone_packages\ ": [", "\" packages\ ": [", "\" tickettype\ ": [", "}]"}, Stringsplitoptions.none)
For each large class processing, first filter, then split into sub-class
. Select (n = n.replace ("\" "," "). Split (new string[] {"},{"}, Stringsplitoptions.none)
. Select (k = = K.replace ("{", "" "). Replace ("}", "")//Sub-class filtering
. Splitasdictionary (":", ","))//Extract the sub-class key-value pairs
. ToArray ()). ToList ();//List
return diclist;

}

(Multi-object) JSON converted to list

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.