【问题标题】:Create List from a String with multiple unnecessary characters从具有多个不必要字符的字符串创建列表
【发布时间】:2013-08-29 10:24:04
【问题描述】:

我正在使用这个字符串:

{"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}}

我想清除此字符串,并生成一个 <List> 或包含相应 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;


        }

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 我已经得出结论,这将是一个有帮助的正则表达式函数。但我不知道从哪里开始
  • 你试过这个:var l = new List<string>(){"Iphone","iPod","Ipad"}; 吗?但说真的不是 JSON 吗?尝试使用 json.net james.newtonking.com/projects/json-net.aspx
  • 您想保留这么多物品中的哪三件?
  • 列表的内容是动态的......所以这只是一个例子

标签: c# asp.net linq


【解决方案1】:
using Newtonsoft.Json.Linq;

string 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}}
";

JObject obj = JObject.Parse(json);

int length = (int)obj.GetValue("length");
var titles = new List<string>();
for (int i = 0; i < length; i++)
{
    JObject item = (JObject) obj.GetValue(i.ToString());
    titles.Add((string) item.GetValue("title"));
}

不要忘记添加 Json.Net nuget:http://www.nuget.org/packages/Newtonsoft.Json/

【讨论】:

  • 我应该为 JObject 使用什么程序集?我正在服用 JObject 在当前上下文中不存在
【解决方案2】:

这是一种更 .NETish 的方法:

使用 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2021-01-29
    • 1970-01-01
    • 2016-02-13
    相关资源
    最近更新 更多