2011-08-19 136 views
0

我有C#WCF服務。我想將其轉換爲JSON包含更多表的JSON String.DataSet結果一些錯誤的JSON格式。JSON格式 - 將數據集轉換爲JSON

{ 
"Target1": [ 
    { 
     "BusinessUnit": "MASS", 
     "RetailerCode": "TEST0002" 
    }, 
    { 
     "BusinessUnit": "MASS", 
     "RetailerCode": "TEST0008" 
    } 
] 
}{ 
"PDCheque1": [ 
    { 
     "BusinessUnit": "MASS", 
     "AccountCode": "TEST0003" 
    } 
] 
} 0 { 
"OutStanding1": [ 
    { 
     "BusinessUnit": "MASS", 
     "Year": "2010" 
    }, 
    { 
     "BusinessUnit": "MASS", 
     "Year": "2010" 
    } 
] 

}

在這裏被coverting方法:

//Converting dataset to json 
    public String ConverTableToJson(DataSet dsDownloadJson){ 

     String tableData = ""; 
     StringBuilder Sb = new StringBuilder(); 
     Sb.Append("{\""); 

     foreach (DataTable dt in dsDownloadJson.Tables) 
     { 
      DataTable dtDownloadJson = dt; 
      string[] StrDc = new string[dtDownloadJson.Columns.Count]; 
      string HeadStr = string.Empty; 

      Sb.Append(dtDownloadJson.TableName + "1\" : ["); 

      if (dtDownloadJson.Rows.Count > 0) 
      { 
       for (int i = 0; i < dtDownloadJson.Columns.Count; i++) 
       { 
        StrDc[i] = dtDownloadJson.Columns[i].Caption; 
        HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\","; 
       } 
       if (HeadStr.Length > 0) 
       { 
        HeadStr = HeadStr.Substring(0, HeadStr.Length - 1); 

        for (int i = 0; i < dtDownloadJson.Rows.Count; i++) 
        { 

         string TempStr = HeadStr; 
         Sb.Append("{"); 

         for (int j = 0; j < dtDownloadJson.Columns.Count; j++) 
         { 
          TempStr = TempStr.Replace(dtDownloadJson.Columns[j] + j.ToString() + "¾", dtDownloadJson.Rows[i][j].ToString()); 
         } 

         Sb.Append(TempStr + "},"); 
        } 

        Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1)); 
       } 
       else 
       { 
        Sb.Append("0 }"); 
       } 


      } 
      else 
      { 
       Sb.Append("0 }"); 
      } 
      Sb.Append("]}"); 

     } 
     return Sb.ToString(); ; 

    } 

如果表中不包含記錄則需要返回 「0」 等`表名[{0}]」

它是這樣來的。它在第二套PDCheque1地方錯了。

我想如何格式化?

在Android中,我希望使用表格名稱來處理每個集合,例如PDCheque1,'Target1`就像智者一樣。

第1套是OK拿到result.When我通過JSONArray array = jsonobject.getJSONArray(tablename);' It say no`PDCheque1``

請幫我...提前

感謝

回答

1

您應該考慮構建JSONObject結構並讓json庫擔心格式化。

Altough,如果你真的想要它做這種方式,那就是:

//Converting dataset to json 
public String ConverTableToJson(DataSet dsDownloadJson) { 
    String tableData = ""; 
    StringBuilder Sb = new StringBuilder(); 
    Sb.Append("{"); 

    foreach (DataTable dtDownloadJson in dsDownloadJson.Tables) { 
     string HeadStr = string.Empty; 

     Sb.Append("\"" + dtDownloadJson.TableName + "1\": ["); 

     for (int j = 0; j < dtDownloadJson.Rows.Count; j++) { 
      Sb.Append("{"); 
      for (int i = 0; i < dtDownloadJson.Columns.Count; i++) { 
       string caption = dtDownloadJson.Columns[i].Caption; 
       Sb.Append("\"" + caption + "\" : \"" + dtDownloadJson.Rows[i][j].ToString() + "\","); 
      } 
      Sb.Append("},"); 
     } 
     Sb.Append("],"); 
    } 
    Sb.Append("}"); 
    return Sb.ToString(); 
} 
1

我真的不明白你的問題,但我會盡力幫助你: 你解析的文件是一個JSONArray。當你解析文件時,它會返回一個jsonTokener。 你需要得到它的jsonarray。

JSONTokener tokener = new JSONTokener(yourfileinString); 
JSONArray array = (JSONArray) tokener.nextValue(); 
JSONObject object = array.getJsonObject(0); // Here you get the object containing the array //Target1 
For PDCheque1, just : 

JSONObject pdcheque = array.getJsonObject(1); 
if(pdcheque.has("PDCheque1")) { 
    JSONArray pdchequeArray = pdcheque.getJSONArray("PDCheque1"); 
    JSONObject pdchequeObject = pdchequeArray.getJSONObject(0); // 0 is the index, there is only one value //here 
    if(pdcheque.has("BusinessUnit") { 
     String businessUnit = pdcheque.getString("BusinessUnit"); 
    } 
} 

如果這不是您所期望的答案,請嘗試重新構造您的問題。

1

不要重新發明輪子。 得到這個庫: http://json.codeplex.com/

而且看看幫助: http://james.newtonking.com/projects/json/help/

這是.NET 4.0。您可以instintiate對象,然後將其序列化:

string output = JsonConvert.SerializeObject(product); 

或者使用JsonWriter級:

using (JsonWriter jsonWriter = new JsonTextWriter(sw)) 
{ 
    jsonWriter.Formatting = Formatting.Indented; 

    jsonWriter.WriteStartObject(); 
    jsonWriter.WritePropertyName("CPU"); 
    jsonWriter.WriteValue("Intel"); 
    jsonWriter.WritePropertyName("PSU"); 
    jsonWriter.WriteValue("500W"); 
    jsonWriter.WritePropertyName("Drives"); 
    jsonWriter.WriteStartArray(); 
    jsonWriter.WriteValue("DVD read/writer"); 
    jsonWriter.WriteComment("(broken)"); 
    jsonWriter.WriteValue("500 gigabyte hard drive"); 
    jsonWriter.WriteValue("200 gigabype hard drive"); 
    jsonWriter.WriteEnd(); 
    jsonWriter.WriteEndObject(); 
} 

樂趣。