简体   繁体   English

解析来自服务器的非常复杂的 JSON 响应,用于 android 应用程序

[英]parsing a very complicated JSON response from server for android app

I made a successfully connection to a remote server and the response i got was 15,000 words JSONObject.我成功连接到远程服务器,得到的响应是 15,000 字 JSONObject。 I'm pretty good with JSON parsing but this is nothing like I've seen before.我很擅长 JSON 解析,但这与我以前见过的完全不同。 this is a very small part of the JSON response i'm getting :这是我得到的 JSON 响应的一小部分:

\\":\\"DI_Orders\\",\\"Query\\":\\"SELECT * from TBL_Orders\\",\\"DataSourceID\\":\\"29cdf9d8-b6fe-4ea5-ac14-e5860a9494db\\",\\"Columns\\":[{\\"@ID\\":\\"C1\\",\\"@DataType\\":\\"System.String\\",\\"ColumnName\\":\\"Branch\\"},{\\"@ID\\":\\"C2\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"DelayInDays\\"},{\\"@ID\\":\\"C3\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"OrderID\\"},{\\"@ID\\":\\"C4\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"ProductID\\"},{\\"@ID\\":\\"C5\\",\\"@DataType\\":\\"System.String\\",\\"ColumnName\\":\\"ProductName\\"},{\\"@ID\\":\\"C6\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"Quantity\\"},{\\"@ID\\":\\"C7\\",\\"@DataType\\":\\"System.String\\",\\"ColumnName\\":\\"OrderStatus\\"}],\\"PrimaryKeyColumns\\":{\\"Column\\":{\\"@ID\\":\\"C7\\"}}},{\\"@ID\\":\\"231c9c51-00e3-4141-bf38-dae6f08d861c\\",\\"@StructureType\\":\\"Regular\\",\\"@Type\\":\\"Query\\",\\"@DataSourceType\\":\\"Sql\\",\\"DataItemName\\":\\"DI_DailySales\\",\\"Query\\":\\"SELECT * from DailySales\\",\\"DataSourceID\\":\\"29cdf9d8-b6fe-4ea5-ac14-e5860a9494db\\",\\"Columns\\":[ \\":\\"DI_Orders\\",\\"Query\\":\\"SELECT * from TBL_Orders\\",\\"DataSourceID\\":\\"29cdf9d8-b6fe-4ea5-ac14-e5860a9494db\\",\\"Columns\\": [{\\"@ID\\":\\"C1\\",\\"@DataType\\":\\"System.String\\",\\"ColumnName\\":\\"Branch\\"},{\\"@ID\\" :\\"C2\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"DelayInDays\\"},{\\"@ID\\":\\"C3\\",\\ "@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"OrderID\\"},{\\"@ID\\":\\"C4\\",\\"@DataType\\":\\" System.Int32\\",\\"ColumnName\\":\\"ProductID\\"},{\\"@ID\\":\\"C5\\",\\"@DataType\\":\\"System.String\\",\\" ColumnName\\":\\"ProductName\\"},{\\"@ID\\":\\"C6\\",\\"@DataType\\":\\"System.Int32\\",\\"ColumnName\\":\\"Quantity\\ "},{\\"@ID\\":\\"C7\\",\\"@DataType\\":\\"System.String\\",\\"ColumnName\\":\\"OrderStatus\\"}],\\"PrimaryKeyColumns\\ ":{\\"列\\":{\\"@ID\\":\\"C7\\"}}},{\\"@ID\\":\\"231c9c51-00e3-4141-bf38-dae6f08d861c\\",\\" @StructureType\\":\\"Regular\\",\\"@Type\\":\\"Query\\",\\"@DataSourceType\\":\\"Sql\\",\\"DataItemName\\":\\"DI_DailySales\\",\\ "查询\\":\\"SELECT * from DailySales\\",\\"DataSourceID\\":\\"29cdf9d8-b6fe-4ea5-ac14-e5860a9494db\\",\\"Columns\\":[

it looks like some type of SQL.. does anybody knows what that is?它看起来像某种类型的 SQL ......有人知道那是什么吗? or how do i parse it to get specific details?或者我如何解析它以获得特定的细节? I have no idea what to do with this long response..我不知道如何处理这么长的回复..

It will be extremely likely that this structure might change since its quite big and it is probably mapping database tables directly, meaning modifying the schema will introduce changes to the response and also your parsers.这个结构很可能会改变,因为它非常大,而且它可能直接映射数据库表,这意味着修改架构将引入响应和解析器的更改。 Be careful not to fail with the parsing if something changes.如果发生变化,请注意不要解析失败。

I pretty much always use Jackson mapper to map the JSON responses to specific Java Models,我几乎总是使用 Jackson 映射器将 JSON 响应映射到特定的 Java 模型,

But in your case i don't think you should parse the whole thing, i would have a model which will just parse the things you need out of this response as opposed to everything.但在你的情况下,我认为你不应该解析整个事情,我会有一个模型,它只会从这个响应中解析你需要的东西,而不是一切。

It will save you a lot of time writing the parsers and debbuging if something changes.如果发生变化,它将为您节省大量编写解析器和调试的时间。 Feels more maintainable not to get the whole thing.感觉更容易维护而不是得到整个事情。 Eventually as you require more things you can parse more stuff out.最终,当你需要更多的东西时,你可以解析出更多的东西。

One other option is just to put it in a json object and use it as a huge HashMap, just extract values where needed from keys.另一种选择是将它放在一个 json 对象中并将其用作一个巨大的 HashMap,只需从键中提取需要的值。 This is pretty much opposite of what i would ever do, but might work better in your case.这与我所做的几乎相反,但在您的情况下可能会更好。

On a side note, whoever wrote the webservice which responds like this should be shot :)附带说明一下,无论谁编写了像这样响应的网络服务,都应该被枪杀:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM