C
C#2y ago
jgomezal

✅ [RESOLVED] Newtonsoft :: wrong serialization format

Dear all, I'm requesting your help, because I don't understand what I'm doing wrong.
string operations = "{\"inumoper\": \"44444\", \"itdoper\": \"ORD1\"}";

Dictionary<string, string> objParams = new Dictionary<string, string>
{
{"accion", "LOAD" },
{"operaciones", operations},
};

string[] arrParams = new string[4];
arrParams[0] = JsonConvert.SerializeObject(objParams, Formatting.None);
arrParams[1] = "123456789";
arrParams[2] = "1001";
arrParams[3] = "0";

Dictionary<string, Array> objSend = new Dictionary<string, Array>
{
{"_parameters", arrParams }
};
string operations = "{\"inumoper\": \"44444\", \"itdoper\": \"ORD1\"}";

Dictionary<string, string> objParams = new Dictionary<string, string>
{
{"accion", "LOAD" },
{"operaciones", operations},
};

string[] arrParams = new string[4];
arrParams[0] = JsonConvert.SerializeObject(objParams, Formatting.None);
arrParams[1] = "123456789";
arrParams[2] = "1001";
arrParams[3] = "0";

Dictionary<string, Array> objSend = new Dictionary<string, Array>
{
{"_parameters", arrParams }
};
` I need this data with this format:
"{\"_parameters\":[{\"accion\":\"LOAD\",\"operaciones\":[{\"inumoper\":\"44444\",\"itdoper\":\"ORD1\"}]},\"123456789\",\"1001\",\"0\"]}"
"{\"_parameters\":[{\"accion\":\"LOAD\",\"operaciones\":[{\"inumoper\":\"44444\",\"itdoper\":\"ORD1\"}]},\"123456789\",\"1001\",\"0\"]}"
But I'm getting it like this:
"{\"_parameters\":["{\"accion\":\"LOAD\",\"operaciones\":\"{\\\"inumoper\\\": \\\"44444\\\", \\\"itdoper\\\": \\\"ORD1\\\"}\"}"]},\"123456789\",\"1001\",\"0\"]}"
"{\"_parameters\":["{\"accion\":\"LOAD\",\"operaciones\":\"{\\\"inumoper\\\": \\\"44444\\\", \\\"itdoper\\\": \\\"ORD1\\\"}\"}"]},\"123456789\",\"1001\",\"0\"]}"
` As you see, in the "operaciones" array, I'm receiving an escape for a backslash and I don't need it. Do you know how can I solve it? Thanks in advance, Migue
8 Replies
Angius
Angius2y ago
Just use a proper class operations is a string, so the serializer will treat it as such Make it an array, and it'll be treated as an array
jgomezal
jgomezalOP2y ago
@Angius sorry this question, but I need to do it: if I'm not wrong the Array doesn't allow me to use custom Key (index), but I need to have the array like this: ["inumoper" = "4444"]
Angius
Angius2y ago
Use an object
class Root
{
public Params _parameters { get; set; }
}
class Params
{
public string accion { get; set; }
public string[] operations { get; set; }
}
class Root
{
public Params _parameters { get; set; }
}
class Params
{
public string accion { get; set; }
public string[] operations { get; set; }
}
var data = new Root {
_parameters = new Params {
accion = "Load",
operations = new[]{"inumoper", "444444", "itdoper"}
}
}
var data = new Root {
_parameters = new Params {
accion = "Load",
operations = new[]{"inumoper", "444444", "itdoper"}
}
}
Or whatever other structure you need Could even use $jsongen
MODiX
MODiX2y ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Angius
Angius2y ago
And generate the classes from your desired JSON
jgomezal
jgomezalOP2y ago
Thank you so much. I will try. @Angius and @MODiX With the clarification about the object, I used this process: Instead of creating an string array, I created the JObject array, so, in the serialization step, it took it as an Object.
string operations = "{\"inumoper\": \"" + (this.orderNumber) + "\", \"itdoper\": \"ORD1\"}";
JObject[] operationsArray = new JObject[1];
operationsArray[0] = JObject.Parse(operations);
string operations = "{\"inumoper\": \"" + (this.orderNumber) + "\", \"itdoper\": \"ORD1\"}";
JObject[] operationsArray = new JObject[1];
operationsArray[0] = JObject.Parse(operations);
It worked as I expected.
Angius
Angius2y ago
Why do you still insist on not using type-safe ways of creating JSON? Still a ton of strings instead of using classes Oh well, whatever works for you ig
jgomezal
jgomezalOP2y ago
To be honest, because I'm learning, and sometimes creating a lot of classes breaks my mind. But I will try to do it that way. I need to practice that part to use classes for getting the objects as you recommend me.
Want results from more Discord servers?
Add your server