在过去的几个小时里,我一直在努力解决这个问题,看起来我的想法只是越来越复杂,虽然我很接近,所以我认为我应该寻求帮助。
我在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; }
}
我想在JSONObject
中创建一个可以解析为该格式的Android
,但我不知道该怎么做..
这是我到目前为止(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
理论上,这几乎是&#34;以所需的格式创建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
例外是:
无法转换类型为&#39; System.String&#39;的对象输入 &#39; System.Collections.Generic.List`1 [MyImages]&#39;
java代码中生成的JSON如下所示:
{&#34;图像&#34;:&#34; {sl_big_ass_logo.png = iVBORw0KGgoAAAANSUhEUgAAAL8AAAC / CAIAAAAgiJpRAAAAAXNSR0IArs4c6QAAAARnQU1BAACx \ njwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAmCSURBVHhe7Z1PbxNHGMb5Jv0YPfQT9NaP0K9Q \ n5dw7l1aoJ9Qeqh44UlXigOBQVcoBpEhUogUERFWCgOD8cezEWZwYaJ9mhtUysde7777v7Izz / PQC \ nUGLPrplfZt5Zz9pXrv5897Mvv2WYtln7 / tcrwY8Ypnm8Pf8S0gbaQ + TQHiKH9hA5tIfIoT1EDu0h \ nc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YuCPGhHaY2gPULyla2kwlx0cbce5AcNBe2ztUbwv \ np3kwY8ZZl9EeW3tAzOGnmpf2XzpBe8ztKaajoF9jxrQkoj3m9gCL686tYrS8pz0x7EEla3rtp2Fw \ nDiiJFMtq2hPDHtBL + bwoWuMQ7YlkD4hw8bB5VOYy2hPPHtB7ARQENXWXfdO0J6o9QPcLjlUCp2XF \ nEO2JbQ9IUCBEMJHRnh7sAYKPL40QDEL + / JpBe / qxB6T59QOopk9nhT / FZdCe3uwBWMancB0oCE6p \ n4VsctKdPewDK1dQWYi5NBKI9PdvjGBeDBAehpQLRniTscSR1OdGlfqcH7UnIHoCKNamJDCNizaUg \ n2pOWPQ78xafjUM1NZLQnRXsc6Ti06EIi7UnXHgccSuHS4tyLQLQndXscKD76rannXoamPXnYU4JJ \ NPK + 1 / cX1F + 3JzB7H5O1 + / M32F4cf2pOlPY74ZXUw / NCejO1xNPyWP5XgQP6o59Ce7O1xxBmHUHL5 \ n451De1bEHgccsq6HqjtZac9K2ePAuizocsUcHG37w9CelbQHoBgyGoSqb1zQntW0x2G0gdq3TntW \ n2x5 gIVC5bqc9K24PUBeI9pSsvj0z7bvoy7fcac / q2wMmqnfR056SS2EPUFyC0Z6Sy2KP4kUg2lNy \ nWexBqRtIIA7tKbks9oBAAnFoTwntaR3aU0J7Wof2lNCe1invMaU9tKd1eK255LLYo7jmKu8upT2X \ nxR7FjwvyLdIeO3vGxQDVZc1N4JHRutZcvbOC9pjYU31Xcmf4pHeHFKct7i2som8Phpzgfxwylavc \ nXlDcMM99zVX07Vm0nwZzR5cPRxaDOTQ4ky7xjZ5De / TtCf67g2AYKFe8ETidFcEJdAlmYd / uObRH \ n2Z6GvRXHIZyM7r6w4LPoaI + yPa3qU8xlSz8bUIzuhIUEtwIC2tOnPS7old3RpmJJhHOwuK / 0YuFP \ NE / q3p4zTqMtohOca3YVzceABtCche6rBpIYSFX / uGJPQJioYf4BPwa9gDB5mMdhUM / EKA + 1J1J6k \ nApX9y / sU2kN7lmdRTUZ7aM + SBNd4qtAe2lOXucVyCe2hPXVZVK07aI + yPSDogHyz9NoB7dG3R2sn \ nTb9pctmJ9ujb0 + / Hcquk4RVL2qNvT + 6lT0N1AO3RtwdkOvxghdXq7TbaY2IPyE6grcFG2x20tMfK \ nHpDm90XOjWzjLO0xtAfgrzmF70eqycv9h / UXdWqgPbb2OFBHW78HLkj3vWm0J4Y9DjiUSDGEEqej \ nNw7aE8 + eEtRDRnu46uN2n0Fifx6doT092ONASQSNIlRFThqVwSaA9vRmTxWMB1j1wCStNRracVsT \ nxRVxE2hPEvZUwZjkZEJQayP1b5zht + 5h7il4brQbn2lPcvZkBO2hPXJoD + 2RQ3tojxzaQ3vkaNlz \ N4 / YDCBQtf23u + BfQGdojR8ue + Fm7duu4mPqX0QHaIydfe5C795 / 6l9GBePbMZrPTc44 + MhwOD1ri \ nn3l0NJ1O0dTZ2ZlvvQ + ytgfDj38ZHbCyB / 1aFAW6GV2 + u7v72pLBYICjjMfjk5MTKOX PwJ6s7UEO \ nxif + lUhRtgfSoBffvHnjO7YPcPTRaBRBo9ztwfn7VyJF0x6MNL4D0wAO + TOzIXd7sP7yr0SKpj2Y \ nPny / pcHe3p4 / Mxtoj6Y9Hz58mEwm1lVOE1AJYSB8 // 69PzMbaI + mPSVYXkEjTBwxTYIxh4eHx8fH \ n0RZitMfEngB0J2pY + PT / Uvt8FeYQFNf + MR + X7mgTLUcokOdCe2LYs6rQHtojh / bQHjm0h / bIORif \ nBP2RV7Z3hv6VSKE9nVi7divokoyCsdO / DCm0pxN37z8NuiSj + NfQAdrTlUyHn9 / ++女/ gA7Qnq6g \ N + RL + 817QN + MN + 7QFaI8Oz7b38NecxTiEk9TanEp7iBzaQ + TQHiKH9hA5tIfIoT1EDu0hcmgPkUN7 \ niBzaQ + TQHiKH9hA5tIfIoT1EDu0hcmgPkUN7iBzaQ + TQHiKH9hA5tIfIoT1EDu0hcj6xh2EEufLV \ nNz8GP2KYJvni6 + / + A1J / rql92ebRAAAAAElFTkSuQmCC \ N}&#34;&#34;名称&#34;:&#34;测试&#34;&#34;姓& #34;:&#34;测试&#34;}
答案 0 :(得分:1)
我不太了解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
。
由于myParameters
的类型为Map<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's documentation