作者:钟杰辉_576 | 来源:互联网 | 2023-10-12 16:44
ImusingJackson1.9.5inanAndroidprojecttoparseJSONfiles.我在Android项目中使用Jackson1.9.5来解析JSO
I'm using Jackson 1.9.5 in an Android project to parse JSON files.
我在Android项目中使用Jackson 1.9.5来解析JSON文件。
So far I haven't had any problems, and can parse files fine using the following code:
到目前为止,我没有遇到任何问题,可以使用以下代码解析文件:
AssetManager mgr = getAssets();
ObjectMapper mapper = new ObjectMapper();
try {
InputStream ifp = mgr.open("detail_schema.json");
schema = mapper.readValue(ifp, DetailSchema.class);
} catch (IOException e) {
e.printStackTrace();
}
Where the DetailSchema class consists of a mix of primitive types and classes. I'm now running into a problem where I want to parse some JSON like the following:
DetailSchema类由原始类型和类组合而成。我现在遇到一个问题,我想解析一些JSON,如下所示:
"fields": {
"Suburb": "Paddington",
"State": "NSW",
"Post Code": "2074",
"Lollipop": "Foo Bar Haz"
}
Where I can't possibly know the map keys before hand (they can be user-defined). As such, I'm not sure what the associated Java class should look like.
我不可能事先知道地图键(他们可以是用户定义的)。因此,我不确定关联的Java类应该是什么样子。
Ie, for this example, it could look like:
即,对于这个例子,它可能看起来像:
public class MyClass {
public String Suburb;
public String State;
public String PostCode;
public String Lollipop;
}
But this may not be correct for another instance of the JSON file. Ideally I need some way for Jackson to map values to something like a NameValuePair. I suspect that the automatic object mapping may not be an option in this case - can someone confirm or deny this?
但是对于JSON文件的另一个实例,这可能不正确。理想情况下,我需要一些方法让Jackson将值映射到类似NameValuePair的东西。我怀疑在这种情况下自动对象映射可能不是一个选项 - 有人可以确认或否认这个吗?
1 个解决方案