So I've done my best to research similar issues like this, but I'm fairly new to Java and JSON, so I apologize if this is redundant. I'm working on a simple text based RPG for my Java class this semester, and I want to store character information and location information in either XML or JSON. I've given both a try, and seem to be getting farther with JSON and Jackson. I've written some tester code to try this out before implementing in my game, but basically I want each game "location" to have an integer ID#, some string information, and a list of exitNodes (which will eventually correspond to other locations in the game). Here is an example of my JSON:
我已经尽力研究类似的问题,但是我对Java和JSON还不熟悉,如果这是多余的,我很抱歉。我这学期在为我的Java类编写一个基于RPG的简单文本,我想在XML或JSON中存储字符信息和位置信息。我已经尝试过这两种方式,并且似乎在JSON和Jackson之间取得了进展。在我的游戏中实现之前,我已经编写了一些测试代码来进行测试,但是基本上我希望每个游戏的“位置”都有一个整数ID#、一些字符串信息和一个exitnode列表(它最终将对应于游戏中的其他位置)。下面是我的JSON示例:
{
"1":{
"enterInfo":"Test Location Information",
"exitNodes":[2,3]
},
"2":{
"enterInfo":"More Test Location Info",
"exitNodes":[4,5]
},
"3":{
"enterInfo":"More Test Location Info",
"exitNodes":[6,7]
}
}
I'm guessing I could organize my JSON a bit better, but ideally I want to be able to grab an object by it's number, and get the "enterInfo" and "exitNodes" back. I've spent hours trying different things without success, but the best I can do is grab the whole JSON document as a JsonNode which just gives me the entire structure, but not an individual object.
我想我可以更好地组织我的JSON,但理想情况下,我希望能够通过它的数字获取一个对象,然后返回“enterInfo”和“exitNodes”。我花了几个小时尝试不同的东西,但没有成功,但我能做的最好的事情是将整个JSON文档作为一个JsonNode,它只提供给我整个结构,而不是一个单独的对象。
Here is a test class (minus constructor/getters/setters/methods) I'm trying to get the JSON into:
这是一个测试类(减去构造函数/getter /setter /方法),我正在尝试将JSON放入:
public class ObjectTest{
int id;
String enterInfo;
int[] exitNodes;
}
And here is what I have working so far, I realize that I'm not even close to achieving my goal here, but I'm out of ideas and the documentation is starting to get confusing, so I've removed my attempts to isolate one of the objects in the JSON file and instantiate an "ObjectTest" object:
这是我工作到目前为止,我意识到我不接近实现我的目标,但是我的想法和文档开始混乱,所以我试图隔离删除一个对象的JSON文件并实例化一个“对象”对象:
package jacksontester;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
public class JackSONTester {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode localeTemp = mapper.readTree(new File("src/jacksontester/TestMeJSON.json"));
System.out.println(localeTemp);
}
}
Not that it really matters, but here is what prints out:
并不是说它真的重要,但以下是打印出来的东西:
{"1":{"enterInfo":"Test Location Information","exitNodes":[2,3]},"2":{"enterInfo":"More Test Location Info","exitNodes":[4,5]},"3":{"enterInfo":"More Test Location Info","exitNodes":[6,7]}}
What I want to achieve is being able to create an "ObjectTest" object with the values from my JSON. Again, sorry if this is a duplicate, I've read through numerous posts already, but I'm new here...
我想要实现的是能够创建一个带有JSON值的“ObjectTest”对象。再说一遍,对不起,如果这是副本,我已经读了很多文章,但我是新来的……
EDIT: I'm realizing now that my JSON file should probably be organized more like this:
编辑:我现在意识到我的JSON文件应该更有条理:
{"locations":[
{ "id":0,
"enterInfo":"Test Location Information",
"exitNodes":[1,2]
},
{ "id":1,
"enterInfo":"More Test Location Info",
"exitNodes":[2,3]
},
{ "id":2,
"enterInfo":"More Test Location Info",
"exitNodes":[4,5]
}
]
}
I would need to then create a list of objects defined by the JSON?
然后我需要创建一个由JSON定义的对象列表?
1 个解决方案
#1
9
This should solve it for you:
这应该能帮你解决这个问题:
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class JacksonTester {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<ObjectTest> myObjects = mapper.readValue(jsonFile(), new TypeReference<List<ObjectTest>>(){});
System.out.println(myObjects);
}
private static File jsonFile() {
return new File("src/main/resources/test.json");
}
}
Using this as JSON:
使用这个JSON:
[{
"id":0,
"enterInfo":"Test Location Information",
"exitNodes":[1,2]
},
{ "id":0,
"enterInfo":"Test Location Information",
"exitNodes":[1,2]
}]
参考
EDIT
Forgot to say that the fields need to have setters or to be public
编辑忘记说字段需要设置setter或公开。
#1
9
This should solve it for you:
这应该能帮你解决这个问题:
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class JacksonTester {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<ObjectTest> myObjects = mapper.readValue(jsonFile(), new TypeReference<List<ObjectTest>>(){});
System.out.println(myObjects);
}
private static File jsonFile() {
return new File("src/main/resources/test.json");
}
}
Using this as JSON:
使用这个JSON:
[{
"id":0,
"enterInfo":"Test Location Information",
"exitNodes":[1,2]
},
{ "id":0,
"enterInfo":"Test Location Information",
"exitNodes":[1,2]
}]
参考
EDIT
Forgot to say that the fields need to have setters or to be public
编辑忘记说字段需要设置setter或公开。