考虑到您的JSON看起来像如下:
{
"Red": {
"Level 1": "Specify Action",
"Level 2": "Action Taken",
"Level 3": "No Action Taken"
},
"Orange": {
"Level 4": "Defeat Gannon",
"Level 5": "Save Princess",
"Level 6": "Find Triforce"
}
}
我将使用HashMap的,像下面杰克逊API。我认为类似的也可以使用GSON来完成
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{\n" +
" \"Red\": {\n" +
" \"Level 1\": \"Specify Action\",\n" +
" \"Level 2\": \"Action Taken\",\n" +
" \"Level 3\": \"No Action Taken\"\n" +
" },\n" +
" \"Orange\": {\n" +
" \"Level 4\": \"Defeat Gannon\",\n" +
" \"Level 5\": \"Save Princess\",\n" +
" \"Level 6\": \"Find Triforce\"\n" +
" }\n" +
"}";
TypeReference< Map<String, HashMap<String, String>>> typeRef
= new TypeReference<Map<String, HashMap<String, String>>>() {
};
Map<String, HashMap<String, String>> value = mapper.readValue(jsonInString, typeRef);
System.out.println(value);
}
}
如何在HashMap函数中传递这个有效载荷(如果它没有变量名称的话)? – petryuno1
对不起,我不清楚。我只想到我可以使用HashMap来表示“红色”和“橙色”对象,但正如您所指出的,由于我不确定如何引用父对象,因此无法传递整体有效内容。 – killQuotes
父母对象是什么意思? –