0

I am receiving bad JSON from server which is a string of actual array so I was using JSON.parse. I looked the asp code and I found that developers are converting list to JSON. What is the best way to encode list to JSON format?

Data that I am getting : {d:"[\\"[5970,5971,5972,5973,5976,5974,5975,5977,5978],[343,232]\\"]"}

Data should look like this : {d:[[5970,5971,5972,5973,5976,5974,5975,5977,5978],[343,232]]}

This is a tiny bit of JSON but it is 5MB of JSON so parsing takes lot of time.

I will really appreciated if someone can help me to find out solution for this as I am a UI guy. http://jsfiddle.net/aavUA/19

The asp code is which is actually calling c sharp function:

public static string GetLevelChildsList(string strInputString)
    {
        List<IndexReleationship> inflationRelationship = SessionManager.InflationRelation;
        string[] inputArguments = strInputString.Split('~');
        int intInflationModelLevelID = int.Parse(inputArguments[0].Trim());
        List<string> lstResultString = new List<string>();
        List<List<int>> strList = new List<List<int>>();
        List<int> strInflationHideIdList = new List<int>();
        List<int> strInflationShowIdList = new List<int>();
        List<int> strActualLevelids = new List<int>();
        List<int> selectedLevelids = new List<int>();
        for (int count = 0; count < inflationRelationship.Count; count++)
        {
            if (inflationRelationship[count].IsReleatedIndex > intInflationModelLevelID)
            {
                if (!strInflationHideIdList.Contains(inflationRelationship[count].ParentIndexID))
                {
                    strInflationHideIdList.Add(inflationRelationship[count].ParentIndexID);
                }

                if (!strInflationHideIdList.Contains(inflationRelationship[count].ChildIndexID))
                {
                    strInflationHideIdList.Add(inflationRelationship[count].ChildIndexID);
                }

            }
            else if (inflationRelationship[count].IsReleatedIndex == intInflationModelLevelID
                     && inflationRelationship[count].IsReleatedIndex != 1169)
            {
                if (!strActualLevelids.Contains(inflationRelationship[count].ChildIndexID))
                {
                    strActualLevelids.Add(inflationRelationship[count].ChildIndexID);
                }
            }
            else
            {
                if (!strInflationShowIdList.Contains(inflationRelationship[count].ParentIndexID))
                {
                    strInflationShowIdList.Add(inflationRelationship[count].ParentIndexID);
                }

                if (!strInflationShowIdList.Contains(inflationRelationship[count].ChildIndexID))
                {
                    strInflationShowIdList.Add(inflationRelationship[count].ChildIndexID);
                }
            }

        }

        strList.Add(strInflationHideIdList);
        strList.Add(strInflationShowIdList);
        strList.Add(strActualLevelids);

        selectedLevelids.AddRange(strInflationShowIdList);
        selectedLevelids.AddRange(strActualLevelids);

        string strResult = GetSessionInflationModels(selectedLevelids);
        lstResultString.Add(strList.ToJson());
        lstResultString.Add(strResult);
        return lstResultString.ToJson();
    }
5
  • You can go to this link to see the problem: jsfiddle.net/aavUA/19 Commented Jan 10, 2012 at 22:13
  • Are you trying to fix the the ASP.NET service that returns the bad JSON, or figure out how to use the bad JSON in your javascript? Commented Jan 10, 2012 at 22:19
  • I am trying to fix the asp code so that it returns healthy JSON and js doesn't have to do extra evaluations. Commented Jan 10, 2012 at 22:29
  • Can you post the current C# or VB.NET code you are using to generate the JSON string? We can probably get you an answer quicker if we can see what the current code is doing wrong. Commented Jan 11, 2012 at 4:43
  • I added c# function to my question. I will appreciated any kind of input. Commented Jan 11, 2012 at 15:52

1 Answer 1

1

I would recommend using the JSON.NET library for serializing your list to JSON, instead of trying to create the JSON yourself. It's pretty much the standard way of dealing with JSON in .NET these days:

http://json.codeplex.com/

You could then create an object like this (I'm going to ignore some common .NET conventions to keep this example simple, such as capitalization)

public class MyObject
{
    public List<List<int>> d { get; set; }
}

and populate that with your values, then use:

MyObject myObj = new MyObject();
myObj.d = strList;
string output = JsonConvert.SerializeObject(myObj);

and the string would be the valid JSON you're trying to return.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you free-dom. I will give this link to my developers team and hope they are able to implement it.
Let me know if they have any trouble. I've used (and still use) the JSON.NET library for a couple of years now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.