0

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);
    }
});
3
  • AJAX is a method of making HTTP calls, not data. In fact X stands for XML. And you don't have a nested array in the IP property. You have an array that contains a single string "[\"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 quotes Commented Dec 1, 2017 at 9:36
  • BTW, why do you export each element manually instead of creating a DTO and serializing it with a single call? Commented Dec 1, 2017 at 9:37
  • I can't reproduce the problem with this code. new 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? Commented Dec 1, 2017 at 10:04

2 Answers 2

1

Your code doesn't generate a nested array, it generates an array that contains a single string value.

It would be a lot easier to generate the JSON string if you created a DTO and serialized it directly. It would also avoid generating so many temporary objects :

class MyDTO
{
    public int type{get;set;}
    public string body {get;set;}
    public int time {get;set;}
    public string[][] ip{get;set;}
    public string hash {get;set;}
    public string exclude {get;set;}
}

void Main()
{
    var myDto=new MyDTO{
        type=2,
        body="Hello",
        time=120000,
        ip=new []{
            new []{
                "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",
        exclude = "127.0.0.1"
    };

    var json=JsonConvert.SerializeObject(myDto,Newtonsoft.Json.Formatting.Indented);
    Console.WriteLine(json);
}

The result is :

{
  "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",
  "exclude": "127.0.0.1"
}

UPDATE 1

If you don't want to use a DTO, you can use an anonymous type. The result will be the same:

    var myDto=new {
        type=2,
        body="Hello",
        time=120000,
        ip=new []{
            new []{
                "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",
        exclude = "127.0.0.1"
    };

You can deserialize the string to the DTO with JsonConvert.DeserializeObject :

var dto=JsonConvert.DeserializeObject<MyDTO>(json);
var fistRange=dto.ip[0][0];

Or you can deserialize to a dynamic object :

    dynamic result=JsonConvert.DeserializeObject<dynamic>(json);
    Console.WriteLine(result.ip[0][0]);

In both cases, the result will be :

192.168.10.1-192.168.10.254

UPDATE 2

The original code does not produce the result shown in the question. It generates a string array property that contains the IP ranges. This code :

    var sendIPs =new []{
                "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"
            };
    var hashString = "6e42b725e351576d123ba7d4fc1b48863e4485821e0edb73abb8801a09a99bc7";                
    JObject data = new JObject(
        new JProperty("type", 2),
        new JProperty("body", "Hello"),
        new JProperty("time", 12000),
        new JProperty("ip", JArray.FromObject(sendIPs)),
        new JProperty("hash", hashString),
        new JProperty("exclue", "127.0.0.1")
    );
    var json = data.ToString();

Returns

{
  "type": 2,
  "body": "Hello",
  "time": 12000,
  "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"
}

Perhaps SendIP contains an already serialized array string?

Sign up to request clarification or add additional context in comments.

4 Comments

Sendip: JSON.stringify(subnet) that serializes the array to a JSON string. Just pass the array. In fact, you could let Web API or MVC (you don't specify which) deserialize the action parameter itself. If your action's signature was exportConf(MyDTO myDto) Web API itself would deserialize the object.
I found the solution, I passed the traditional json parameter true! Thanks anyway !
@Y.op the solution to which question? You never asked about Javascript. This entire question was about .NET which has no json parameter
My problem was that I was sending an array, and I ended up with a character string, I thought it was a C# problem, but it was an ajax option.
0

Maybe perhaps it should be;

new JProperty("ip", JArray.FromObject(SendIp[0]))

3 Comments

@Y.op why are you trying to build the string one element at a time like this? It's easier and faster to serialize a strongly typed object, anonymous type or dynamic object.
"It doesn't work" Would you explain it ? What is the output ?
I have exactly the same thing as before. But I found the solution, I edited my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.