2012-01-10 3 views
0

JSON.parse를 사용하고 있으므로 실제 배열의 문자열 인 서버에서 잘못된 JSON을 수신하고 있습니다. asp 코드를 살펴본 결과 개발자가 목록을 JSON으로 변환하고있는 것으로 나타났습니다. 목록을 JSON 형식으로 인코딩하는 가장 좋은 방법은 무엇입니까? 내가 얻고asp.net의 JSON에 나열

데이터 : {D : "[\\"[5970,5971,5972,5973,5976,5974,5975,5977,5978], [343232] \\ "]"}

이 같아야

데이터 : {D : [5970,5971,5972,5973,5976,5974,5975,5977,5978], [343232]}

이것은 JSON 조금의 있지만 5MB의 JSON이므로 파싱에는 많은 시간이 걸립니다.

내가 UI 사용자 인 것처럼 다른 사람이이 솔루션을 찾을 수 있도록 도와 주시면 정말 감사하겠습니다. http://jsfiddle.net/aavUA/19

ASP 코드는 실제로 C 날카로운 함수를 호출하는 것입니다 :

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(); 
    } 
+0

이 링크를 통해 문제를 볼 수 있습니다 : http://jsfiddle.net/aavUA/19/ – emphaticsunshine

+0

나쁜 JSON을 반환하는 ASP.NET 서비스를 수정하려고합니까, 또는 어떻게 해결할 수 있습니까? 자바 스크립트에서 잘못된 JSON을 사용 하시겠습니까? – apiguy

+0

건강한 JSON을 반환하고 js가 추가 평가를하지 않아도되도록 asp 코드를 수정하려고합니다. – emphaticsunshine

답변

1

내가 JSON에 대한 목록을 직렬화, 대신 JSON 자신을 만들려고위한 JSON.NET 라이브러리를 사용하는 것이 좋습니다. 당신은 다음과 같은 객체를 생성 할 수

http://json.codeplex.com/

(나는 간단한 예제를 유지하기 위해 몇 가지 일반적인 .NET 규칙을 무시하는거야 : 그것은 꽤 많은 요즘 .NET에서 JSON 처리의 표준 방법 t

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

및 문자열은 다음과 같습니다, 같은

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

) 대문자로하고 값이를 채우는 후 사용 그는 JSON을 반환하려고합니다.

+0

무료 DOM을 이용해 주셔서 감사합니다. 개발자 팀에이 링크를 제공하고 구현할 수 있기를 바랍니다. – emphaticsunshine

+0

아무 문제가 있는지 알려주세요. 나는 몇 년 동안 JSON.NET 라이브러리를 사용 해왔다. – apiguy