如何将 JSON 文本文件解析为 BasicDBObject。

huangapple go评论75阅读模式
英文:

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&lt;BasicDBObject&gt; collectionAttributes = (List&lt;BasicDBObject&gt;) exchange.getInput();
        for (BasicDBObject collectionAttribute : collectionAttributes) {
          if (collectionAttribute.get(&quot;name&quot;) != null) {
            String attributeName = collectionAttribute.getString(&quot;name&quot;);
            if (attributeName.equals(JobParamConstants.DEFAULT_LOCALE) || attributeName
              .equals(JobParamConstants.APPLICABLE_LOCALES)) {
              exchange.setProperty(attributeName, collectionAttribute.getString(&quot;_id&quot;));
            }
          }

hi i need to write junit testcase for above program..so i want to pass input to collectionAttributes.my input json is GetCatalogCollectionResponse.json

{
&quot;properties&quot;:[{
&quot;attributeId&quot;:&quot;123&quot;,
&quot;value&quot;:&quot;345&quot;
},
{
&quot;attributeId&quot;:&quot;2345&quot;,
&quot;value&quot;:&quot;567&quot;
}]
}

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(&quot;test_file.json&quot;));
Gson gson = new Gson();
Map&lt;String, Object&gt; 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());
// {&quot;properties&quot;: [{&quot;attributeId&quot;: &quot;123&quot;, &quot;value&quot;: &quot;345&quot;}, {&quot;attributeId&quot;: &quot;2345&quot;, &quot;value&quot;: &quot;567&quot;}]}

答案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(&quot;GetCatalogCollectionAttributeResponse.json&quot;); BasicDBObject collectionAttributes = BasicDBObject.parse(json);`

hope it will helps somebody.

huangapple
  • 本文由 发表于 2020年10月7日 12:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定