JSON Format - convert Dataset to JSON
I have C# WCF service.There I want to convert to JSON String.DataSet contain more table Currntly coming result some wrong json format.
{
"Target1": [
{
"BusinessUnit": "MASS",
"RetailerCode": "TEST0002"
},
{
"BusinessUnit": "MASS",
"RetailerCode": "TEST0008"
}
]
}{
"PDCheque1": [
{
"BusinessUnit": "MASS",
"AccountCode": "TEST0003"
}
]
} 0 {
"Ou开发者_运维百科tStanding1": [
{
"BusinessUnit": "MASS",
"Year": "2010"
},
{
"BusinessUnit": "MASS",
"Year": "2010"
}
]
}
Here is coverting method:
//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(); ;
}
IF the table doesn't contain records then it need to return "0" like `Tablename [{0}]'
Its coming like this.Its wrong in 2nd set PDCheque1
place.
How I want to format?
In Android I want to gt the each set using tablename i.e PDCheque1
,'Target1` like wise .
1st set is ok to get the result.When I pass JSONArray array = jsonobject.getJSONArray(tablename);' It say no
`PDCheque1``
Please help me...
Thanks in advance
you should consider building a JSONObject structure and let the json library worry about the formating.
Altough, if you really want it done this way, here it is:
//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();
}
I don't really understand your question, but i will try to help you : The file you parse is a JSONArray. When you parse the file, it returns a jsonTokener. You need to get the jsonarray of it.
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");
}
}
If it's not the answer that you expected, try to reformulate your question.
Do not reinvent the wheel. Get this Library: http://json.codeplex.com/
And have a look at the help: http://james.newtonking.com/projects/json/help/
It's .net 4.0. You can either instintiate the object and then serialize it:
string output = JsonConvert.SerializeObject(product);
Or use the 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();
}
have fun.
I think it would be easier if you constructed a JSONArray or JSONObject rather than trying to build the string yourself. (see the documentation here : http://developer.android.com/reference/org/json/package-summary.html)
In the end just do myJSONObject.toString();
精彩评论