Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从包含多个不必要字符的字符串创建列表_C#_Asp.net_Linq - Fatal编程技术网

C# 从包含多个不必要字符的字符串创建列表

C# 从包含多个不必要字符的字符串创建列表,c#,asp.net,linq,C#,Asp.net,Linq,我拿着这根绳子: {"0":{"title":"iPhone"},"1":{"title":"iPod"},"2":{"title":"iPad"},"length":3,"prevObject":{"0":{},"1":{},"2":{},"length":3,"prevObject":{"0":{},"length":1,"context":{"location":{},"jQuery18308480830912211994":1},"selector":"#mycart"},"contex

我拿着这根绳子:

{"0":{"title":"iPhone"},"1":{"title":"iPod"},"2":{"title":"iPad"},"length":3,"prevObject":{"0":{},"1":{},"2":{},"length":3,"prevObject":{"0":{},"length":1,"context":{"location":{},"jQuery18308480830912211994":1},"selector":"#mycart"},"context":{"location":{},"jQuery18308480830912211994":1},"selector":"#mycart li"},"context":{"location":{},"jQuery18308480830912211994":1}}
我希望清除此字符串,并生成一个或数组,其中包含相应的3项: Iphone iPod Ipad 我的生成上述内容的代码是:

$("#btngetCart").live("click", function () {

            var items = $('#mycart').find('li').map(function () {
                var item = {};


                item.title = $(this).text();

                return item;
            });
            var json = JSON.stringify(items);

            $.ajax({

                type: 'POST',

                url: "WebForm5.aspx/GetCart",

                data: "{item:" + JSON.stringify(json) + "}",

                contentType: 'application/json; charset=utf-8',

                dataType: 'json',

                success: function (r) {



                }

            });

        });
关于背后的代码:

[WebMethod(EnableSession = true)]
        public static string GetCart(string item)
        {

            HttpContext.Current.Session["f"] = item;
            return item;


        }

别忘了添加Json.Net nuget:

这里有一个更具.Net风格的方法:

使用NuGet安装Json.Net。然后使用类似的方法:

var json = @"{""0"":{""title"":""iPhone""},""1"":{""title"":""iPod""},""2"":{""title"":""iPad""},""length"":3,""prevObject"":{""0"":{},""1"":{},""2"":{},""length"":3,""prevObject"":{""0"":{},""length"":1,""context"":{""location"":{},""jQuery18308480830912211994"":1},""selector"":""#mycart""},""context"":{""location"":{},""jQuery18308480830912211994"":1},""selector"":""#mycart li""},""context"":{""location"":{},""jQuery18308480830912211994"":1}}";
var @object = JObject.Parse(json);

var length = (int)@object["length"];
var indices = Enumerable.Range(0, length);
var titles = indices.Select(index => @object[index.ToString()]["title"]).ToList();

到目前为止,你试过什么吗?我已经得出结论,这将是一个正则表达式函数,将有帮助。但是我不知道从哪里开始你试过这个吗:var l=新列表{Iphone,iPod,Ipad}?但很严重,不是吗?请尝试使用json.net,您希望保留这些对象中的哪3项?列表中的内容以友好方式提供。。。这只是一个例子,我应该用什么程序集来处理JObject?我正在接受当前上下文中不存在的JObject
var json = @"{""0"":{""title"":""iPhone""},""1"":{""title"":""iPod""},""2"":{""title"":""iPad""},""length"":3,""prevObject"":{""0"":{},""1"":{},""2"":{},""length"":3,""prevObject"":{""0"":{},""length"":1,""context"":{""location"":{},""jQuery18308480830912211994"":1},""selector"":""#mycart""},""context"":{""location"":{},""jQuery18308480830912211994"":1},""selector"":""#mycart li""},""context"":{""location"":{},""jQuery18308480830912211994"":1}}";
var @object = JObject.Parse(json);

var length = (int)@object["length"];
var indices = Enumerable.Range(0, length);
var titles = indices.Select(index => @object[index.ToString()]["title"]).ToList();