如何在android中创建这个嵌套JSON对象,并在c#中解码它

本文关键字:并在 对象 解码 JSON android 创建 嵌套 | 更新日期: 2023-09-27 18:19:03

在过去的几个小时里,我一直在努力解决这个问题,尽管我已经接近了,但似乎我的想法只是让它变得越来越复杂,所以我想我应该寻求帮助。

我在C#中有这个类:

public class MyTestObject
{
    public string name { get; set; }
    public string lastname { get; set; }
    public List<MyImages> {get; set; }
}
public class MyImages
{
    public string imageName { get; set; }
    public string imageBytes { get; set; }
}

我想在Android中创建一个可以解析为该格式的JSONObject,但我不确定如何做到这一点。

这是我到目前为止(在Android / Java):

Map<String, String> myParameters = new HashMap<>();
myParameters.put("name", "test");
myParameters.put("lastname", "test");
List<MyImage> images = getListOfImages();
if (images != null && images.size() > 0) {
    Map<String, String> jsonMap = new HashMap<>();
    for (MyImage image : images) {
        jsonMap.put(image.getImageName(), image.getImageBytes());
    }
    myParameters.put("images", jsonMap);
}
JSONObject data = new JSONObject(myParameters);
callWcfService(data); // makes a POST request to the service and passes the JSON object

理论上,这"几乎"创建了所需格式的JSON对象,但是我无法在c#中解码它。

这是WCF服务中应该解码JSON字符串的代码:
var reader = new StreamReader(data); 
var request = reader.ReadToEnd();
request = request.Replace("=", ":"); // i need to replace the `=` that is for some reason added in the JSON with `:`
var parser = new JavaScriptSerializer(); 
var myObject = parser.Deserialize<MyTestObject>(request); // it crashes here

例外是:

不能转换系统类型的对象。字符串'到类型"System.Collections.Generic.List"1(模板)"

从java代码生成的JSON如下所示:

{"图像":"{sl_big_ass_logo.png = iVBORw0KGgoAAAANSUhEUgAAAL8AAAC/CAIAAAAgiJpRAAAAAXNSR0IArs4c6QAAAARnQU1BAACx ' njwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAmCSURBVHhe7Z1PbxNHGMb5Jv0YPfQT9NaP0K9Q ' n5dw7l1aoJ9Qeqh44UlXigOBQVcoBpEhUogUERFWCgOD8cezEWZwYaJ9mhtUysde7777v7Izz/PQc ' nUGLPrplfZt5Zz9pXrv5897Mvv2WYtln7/tcrwY8Ypnm8Pf8S0gbaQ + TQHiKH9hA5tIfIoT1EDu0h ' ncmgPkUN7iBzaQ + TQHiKH9hA5tIfIoT1EDu0hcmgPkUN7iBzaQ + TQHiInb3umZ8eL4h9BLMnGHggx ' nLga7o82X + w8fvbiz/vinJsEj8fiDo208l0qpk7Q96G90/NZgI3CiS9Aa2iymI38M0oEU7UHXYoxp ' nPsCIszN8Mnm7749K2pOWPZhfnr9eD/o4QiAr5zUBqdhzOHkVYbCpDyokDkWt6N8edFjv3lQDhzgO ' nNaRPe2bvTtFVQeclEg5CTejNHpTGSQ05F0OBltKPPVgzB12VYCC3P12ygB7swQIn6Kdkw + Gnntj2 ' nZKQOgpWgP28yj6j25KUOgqLenzqZRzx7xsUg6Jv0Q3vqiWTP9Ow46JgsQnvqiWRPL + 8/dA/tqSeG ' nPVmsz + eG9tRjbk + mc5YL7anH3B50QNAlGYX21GNrj93A8 + jFnZ3hk8PJKxwCmb07dUfEP9xPJm/3 ' n8Vs8pkvJRXvqsbXHYuBBm213BkKpcTGASUFTS0N76jG0B30WdEbHbA02MKj41qVgTGq + 1 zx21gno ' nj + 5 sa3e06dvv4hrwnlnwjehkp4hmw9aexws8uuquyhss39pi97nqsbjhcdqkmh0sgiz560u9vvag ' nvAh6QhyI6Bu1BEcJymru71mKlT1ab6 cbzvmlqfxuhii63nyzfct7tnbqndtsxsqeqajxfhpjcza + ' n2d4Zbjx + sf7nPzduP1gU/BaPeba9h8f7Z + ZM0vZkcbkFNlz95ffPv/6 hbdau3yjmvpu8mbeh1upg ' ngSzp23P95r3AibZBC76tDKE9cnb2jwIVZMGU51vMDdojR8sezH2 + xdygPXJoT9L2IL7FJKE9qduT ' n8vUe2pO6PZGvNbeC9qRuD4LWfLuJQXtM7DmdFYEBXfL89bpvNzFoj4k9IDCgY9JcfNEeK3tqtlzJ ' nkmABRHus7MFoEXR/92 wnnnzraub7row5nlwk + l4lGNLSKaJpj5U9usuuIInMYrTHyh6gX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ioYf4BPwa9gDB5mMdhUM/类+ 1 j1j6k ' nApX9y/sU2kN7lmdRTUZ7aM + SBNd4qtAe2lOXucVyCe2hPXVZVK07aI + yPSDogHyz9NoB7dG3R2sn ' nTb9pctmJ9ujb0 +/Hcquk4RVL2qNvT + 6 lt0n1ao3rtwdkovxghdxq7tbay2ipye6grcfg2x20tmfk ' nHpDm90XOjWzjLO0xtAfgrzmF70eqycv9h/UXdWqgPbb2OFBHW78HLkj3vWm0J4Y9DjiUSDGEEqej ' nNw7aE8 + eEtRDRnu46uN2n0Fifx6doT092ONASQSNIlRFThqVwSaA9vRmTxWMB1j1wCStNRracVsT ' nxRVxE2hPEvZUwZjkZEJQayP1b5zht + 5 h7il4brqbn2lpcvzkbo2hpxjod + 2 rq3tojxzaq3vkanlz '陶瓷/YDCBQtf23u + BfQGdojR8ue + Fm7duu4mPqX0QHaIydfe5C795/6l9GBePbMZrPTc44 + MhwOD1ri ' nn3l0NJ1O0dTZ2ZlvvQ + ytgfDj38ZHbCyB/1 afaw6gv2 + u7v72pLBYICjjMfjk5MTKOXPwJ6s7UEO ' nx如果+ lUhRtgfSoBffvHnjO7YPcPTRaBRBo9ztwfn7VyJF0x6MNL4D0wAO + TOzIXd7sP7yr0SKpj2Y ' nPny/pcHe3p4/Mxtoj6Y9Hz58mEwm1lVOE1AJYSB8//69 pzmbai + mPSVYXkEjTBwxTYIxh4eHx8fH ' n0RZitMfEngB0J2pY + PT/Uvt8FeYQFNf + + X7mgTLUcokOdCe2LYs6rQHtojh/bQHjm0h先生/bIORif ' nBP2RV7Z3hv6VSKE9nVi7divokoyCsdO/DCm0pxN37z8NuiSj + NfQAdrTlUyHn9/+ +ν/gA7Qnq6g ' n + rl + 817 qn +锰+ 7 qfai8oz7b38necxtiek9tanep7ibzaq + TQHiKH9hA5tIfIoT1EDu0hcmgPkUN7 ' niBzaQ + TQHiKH9hA5tIfIoT1EDu0hcmgPkUN7iBzaQ + TQHiKH9hA5tIfIoT1EDu0hcj6xh2EEufLV ' nNz8GP2KYJvni6 +/+ A1J/rql92ebRAAAAAElFTkSuQmCC ' n}"、"名称":"测试","姓":"测试"}

如何在android中创建这个嵌套JSON对象,并在c#中解码它

我不太懂Java,但我认为你的序列化算法是错误的,特别是这一部分:

Map<String, String> jsonMap = new HashMap<>();
for (MyImage image : images) {
    jsonMap.put(image.getImageName(), image.getImageBytes());
}
myParameters.put("images", jsonMap);

"images"不应该包含Map<String, String>而应该包含JsonArray

由于myParametersMap<String, String>类型,您不能将数组添加到哈希映射中,因此我们必须寻找更灵活的解决方案。

尝试以下操作:

JsonObjectBuilder jsonObject = Json.createObjectBuilder();
jsonObject.add("name", "test");
jsonObject.add("lastname", "test");
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for(MyImage image : images)
{
   JsonObjectBuilder imageJson = Json.createObjectBuilder();
   imageJson.add("imageName", image.getImageName());
   imageJson.add("imageBytes", image.getImageBytes());
   jsonArray.add(imageJson.build());
}
jsonObject.add("images", jsonArray.build());
JsonObject data = jsonObject.build();
callWcfService(data);

你的WCF服务应该会根据你给我们看的模型很好地反序列化它。

请注意,我提供的代码没有经过我的测试,而是取自JsonArray的文档