-3

I am taking this string:

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

I would like clear this string, and produce a <List> or array that contains the 3 items accordingly: Iphone iPod Ipad my code that produce the above is:

$("#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) {



                }

            });

        });

and on the code behind:

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

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


        }
focus
  • 171
  • 9
  • 31

2 Answers2

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

Don't forget to add the Json.Net nuget: http://www.nuget.org/packages/Newtonsoft.Json/

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • What is the assembly i should use for JObject? I am taking JObject does not exists in the current context – focus Aug 29 '13 at 10:44
1

Here's a more .NETish way to do it:

Install Json.Net using NuGet. Then use something like this:

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();
Sam
  • 40,644
  • 36
  • 176
  • 219