英文:
how to parse a json text file into BasicDBobject
问题
public void process(FeedExchange exchange) throws Exception {
List<BasicDBObject> collectionAttributes = (List<BasicDBObject>) exchange.getInput();
for (BasicDBObject collectionAttribute : collectionAttributes) {
if (collectionAttribute.get("name") != null) {
String attributeName = collectionAttribute.getString("name");
if (attributeName.equals(JobParamConstants.DEFAULT_LOCALE) || attributeName
.equals(JobParamConstants.APPLICABLE_LOCALES)) {
exchange.setProperty(attributeName, collectionAttribute.getString("_id"));
}
}
}
}
你好,我需要为上述程序编写 JUnit 测试用例,所以我想将输入传递给 collectionAttributes
。我的输入 JSON 是 GetCatalogCollectionResponse.json
:
{
"properties":[
{
"attributeId":"123",
"value":"345"
},
{
"attributeId":"2345",
"value":"567"
}
]
}
我想将这个 JSON 解析到 MongoDB 中的 collectionAttributes
。我尝试了以下代码:
BasicDBObject DBObject = new BasicDBObject();
DBObject.parse(GetCatalogCollectionResponse.json);
但是我得到了一个错误。你能帮助我吗?我是 Java 初学者,非常感谢您的帮助。
英文:
public void process(FeedExchange exchange) throws Exception {
List<BasicDBObject> collectionAttributes = (List<BasicDBObject>) exchange.getInput();
for (BasicDBObject collectionAttribute : collectionAttributes) {
if (collectionAttribute.get("name") != null) {
String attributeName = collectionAttribute.getString("name");
if (attributeName.equals(JobParamConstants.DEFAULT_LOCALE) || attributeName
.equals(JobParamConstants.APPLICABLE_LOCALES)) {
exchange.setProperty(attributeName, collectionAttribute.getString("_id"));
}
}
hi i need to write junit testcase for above program..so i want to pass input to collectionAttributes.my input json is GetCatalogCollectionResponse.json
{
"properties":[{
"attributeId":"123",
"value":"345"
},
{
"attributeId":"2345",
"value":"567"
}]
}
i want to parse this json to collectionAttributes in mongodb.i tried the following code
BasicDBObject DBObject = new BasicDBObject();
DBObject.parse(GetCatalogCollectionResponse.json);
but i got an error.could you help me i am beginner to java,any help would be appreciated..
答案1
得分: 1
你可以使用谷歌的 Gson
库来解析文件中的 JSON,如下所示。解析后的对象可以映射到一个 java.util.Map
,然后你可以构建 BasicDBObject
。
BufferedReader reader = new BufferedReader(new FileReader("test_file.json"));
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(reader, Map.class);
System.out.println(map);
// {properties=[{attributeId=123, value=345}, {attributeId=2345, value=567}]}
BasicDBObject obj = new BasicDBObject(map);
System.out.println(obj.toJson());
// {"properties": [{"attributeId": "123", "value": "345"}, {"attributeId": "2345", "value": "567"}]}
英文:
You can use the Google's Gson
library to parse the JSON from a file as shown below. The parsed object can be mapped to a java.util.Map
, from which you can build the BasicDBObject
.
BufferedReader reader = new BufferedReader(new FileReader("test_file.json"));
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(reader, Map.class);
System.out.println(map);
// {properties=[{attributeId=123, value=345}, {attributeId=2345, value=567}]}
BasicDBObject obj = new BasicDBObject(map);
System.out.println(obj.toJson());
// {"properties": [{"attributeId": "123", "value": "345"}, {"attributeId": "2345", "value": "567"}]}
答案2
得分: 0
终于我找到了我问题的答案。
public class BasicDBObjectInput {
public static String getInput(String resourceName) throws IOException {
ClassLoader classLoader = BasicDBObjectInput.class.getClassLoader();
File file = new File(classLoader.getResource(resourceName).getFile());
String json = FileUtils.readFileToString(file, Charset.defaultCharset());
return json;
}
}
在这里,我们可以像这样将我们的 JSON 文件传递为字符串格式
String json = `BasicDBObjectInput.getInput("GetCatalogCollectionAttributeResponse.json"); BasicDBObject collectionAttributes = BasicDBObject.parse(json);`
希望它能帮助某人。
英文:
finally i found answer for my question.
public class BasicDBObjectInput {
public static String getInput(String resourceName) throws IOException {
ClassLoader classLoader = BasicDBObjectInput.class.getClassLoader();
File file = new File(classLoader.getResource(resourceName).getFile());
String json = FileUtils.readFileToString(file, Charset.defaultCharset());
return json;
}
}
here we can pass our json file in string format like this
String json = `BasicDBObjectInput.getInput("GetCatalogCollectionAttributeResponse.json"); BasicDBObject collectionAttributes = BasicDBObject.parse(json);`
hope it will helps somebody.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论