2012-01-10 10 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サービスを修正しようとしているのですか。あなたのJavaScriptで悪いJSONを使用しますか? – apiguy

+0

健全なJSONを返すようにaspコードを修正しようとしています.jsは余分な評価をする必要はありません。 – emphaticsunshine

答えて

1

私はJSONへのあなたのリストをシリアル化するのではなく、JSONを自分で作成しようとしているためJSON.NETライブラリを使用することをお勧めします。あなたは、このようなオブジェクトを作成することができ

http://json.codeplex.com/

(私は簡単なこの例を維持するためにいくつかの一般的な.NETの規則を無視するつもりです:これはかなりこれらの日.NETでJSONを扱うための標準的な方法ですトン

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

を、文字列は次のようになります。このような

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

)総額とし、自分の価値観を持つことを移入し、使用彼はあなたが返そうとしているJSONを有効にしています。

+0

フリー・ドムありがとうございます。私は開発者チームにこのリンクを提供し、それを実装できることを願っています。 – emphaticsunshine

+0

問題がある場合は教えてください。私は数年前からJSON.NETライブラリを使ってきました(まだ使用しています)。 – apiguy

関連する問題