I'm recovering Ajax data in C#, so I was creating a JSON file that has this format:
{
"type": 2,
"body": "Hello",
"time": 120000,
"ip": [
"[\"192.168.10.1-192.168.10.254\",\"192.168.11.1-192.168.11.254\",\"192.168.12.1-192.168.11.20\",\"192.168.1.5-192.168.1.50\"]"
],
"hash": "6e42b725e351576d123ba7d4fc1b48863e4485821e0edb73abb8801a09a99bc7",
"exclue": "127.0.0.1 "
}
with that :
public void exportConf(int type, string body, int time, string[] SendIp, string lstIp)
{
StringBuilder Sb = new StringBuilder();
byte[] bytes = Encoding.UTF8.GetBytes("test");
SHA256 crypt = SHA256.Create();
byte[] hashBytes = crypt.ComputeHash(bytes);
foreach (byte a in hashBytes)
{
Sb.Append(a.ToString("x2"));
}
JObject data = new JObject(
new JProperty("type", type),
new JProperty("body", body),
new JProperty("time", (time*60*1000)),
new JProperty("ip", JArray.FromObject(SendIp)),
new JProperty("hash", Sb.ToString()),
new JProperty("exclue", lstIp)
);
System.IO.File.WriteAllText(System.IO.Path.GetTempPath()+"xyz.json", data.ToString());
}
I call this function from my JS :
$('.send').click(function () {
var subnet = [];
$('table tbody tr td input[type=checkbox]').each(function () {
if ($(this).is(':checked')) {
subnet.push($(this).val());
}
});
idType = $(".slct-type select :selected").val();
body = $('.zoneEdit textarea').val();
time = $('input[name=timeout]').val();
lstIp = $('.zoneIp textarea').val();
if (subnet.length > 0) {
$.ajax({
url: '/Scripts/exportConf',
type: "POST",
data: { type: idType, body: body, time: time, Sendip: JSON.stringify(subnet), lstIp: lstIp },
dataType: "json",
success: function (data) {
for (i = 0; i < data.length; i++) {
if (data[i].id == idType) {
} else {
}
}
}
});
} else {
alertbox('danger', 'Error', 2000);
}
});
When I parse my JSON file like this :
if (File.Exists(Path.GetTempPath() + "xyz.json"))
{
JObject data = JObject.Parse(File.ReadAllText(System.IO.Path.GetTempPath() + "xyz.json"));
Console.Write(data["ip"]);
}
data[' IP'] should normally be an array, but when I do data[' IP'][0], I get this :
["192.168.10.1-192.168.10.254","192.168.11.1-192.168.11.254","192.168.12.1-192.168.11.20","192.168.1.5-192.168.1.50"]
IP is not treated as an array, but as a single string, I don't know why. Do you have a solution?
EDIT : I've found the solution, you have to go "traditional" to the truth.
http://api.jquery.com/jQuery.param/
Thx !
$('.send').click(function () {
var subnet = [];
$('table tbody tr td input[type=checkbox]').each(function () {
if ($(this).is(':checked')) {
subnet.push($(this).val());
}
});
idType = $(".slct-type select :selected").val();
body = $('.zoneEdit textarea').val();
time = $('input[name=timeout]').val();
lstIp = $('.zoneIp textarea').val();
if (subnet.length > 0) {
$.ajax({
url: '/Scripts/exportConf',
type: "POST",
data: { type: idType, body: body, time: time, Sendip: subnet, lstIp: lstIp },
traditional : true,
dataType: "json",
success: function (data) {
for (i = 0; i < data.length; i++) {
if (data[i].id == idType) {
} else {
}
}
}
});
} else {
alertbox('danger', 'Merci de selectionner un réseau', 2000);
}
});
"[\"192.....50\"]". That string looks like it contains a JSON array, but it's still a string. Remove the double quotes to convert it to an arrya. BTW, you can use single quotes instead of escaping double quotesnew JProperty("ip", JArray.FromObject(sendIPs)),creates a single array of string values, not what was posted here. Did you use.ToString()to convert the JArray to a string perhaps?