我有C#WCF服务。我想要转换为JSON String.DataSet包含更多表。目前结果有些错误的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"
}
]
}
这是隐蔽方法:
//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”,如“Tablename [{0}]”
它就像这样。第二集PDCheque1
地方错了。
我想如何格式化?
在Android中,我想使用tablename(即PDCheque1
)对每个集合进行gt,“Target1”就像明智一样。
第一组可以得到结果。当我通过JSONArray array = jsonobject.getJSONArray(tablename);' It say no
`PDCheque1``
请帮帮我......
提前致谢
答案 0 :(得分: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 :(得分: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();
}
答案 2 :(得分:1)
不要重新发明轮子。 获取此图书馆: http://json.codeplex.com/
看看帮助: http://james.newtonking.com/projects/json/help/
这是.net 4.0。您可以直观地对象然后将其序列化:
string output = JsonConvert.SerializeObject(product);
或者使用JsonWriter-Class:
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();
}
玩得开心。
答案 3 :(得分:0)
我认为如果构建JSONArray或JSONObject而不是尝试自己构建字符串会更容易。 (请参阅此处的文档:http://developer.android.com/reference/org/json/package-summary.html)
最后,只需执行myJSONObject.toString();