java - 使用 java 中的 gson 库将过滤后的 JSON 数据从一个文件复制到另一个文件

标签 java json parsing gson filtering

我想将 JSON 字段从一个文件复制到另一个文件,但前提是该字段满足特定条件,例如

{"dataset":
   [
      {"album_id":1,
       "album_type":"Live Performance",
       "artist_name":"John Doe",....
       }
   ] 
}

我只想复制用户指定的 Artist_name 或任何其他属性的记录,否则跳过元组进行复制。我使用以下代码将过滤后的记录添加到 JSONObject“wr”,然后将其写入输出文件。但它没有给我想要的结果

public static void dumpJSONElement(JsonElement element) {
    if (element.isJsonObject()) {
        JsonObject obj = element.getAsJsonObject();
        java.util.Set<java.util.Map.Entry<String,JsonElement>> entries = obj.entrySet();
        java.util.Iterator<java.util.Map.Entry<String,JsonElement>> iter = entries.iterator();
        while (iter.hasNext()) {
            java.util.Map.Entry<String,JsonElement> entry = iter.next();
            if(entry.getKey().equals(filterKey)){
                if(! entry.getValue().toString().replace("\"", "").equals(filterValue)){
                    wr.put(entry.getKey(), entry.getValue());
                }
            }
            else{
                wr.put(entry.getKey(), entry.getValue());
            }
            dumpJSONElement(entry.getValue());
        }

    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        java.util.Iterator<JsonElement> iter = array.iterator();
        while (iter.hasNext()) {
            JsonElement entry = iter.next();
            dumpJSONElement(entry); 
        }
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive value = element.getAsJsonPrimitive();

    } else if (element.isJsonNull()) {

    } else {
        System.out.println("Error. Unknown type of element");
    }
}

最佳答案

使用下面的代码将 json 字符串转换为通用 java 类型 List<Map<Object, Object>> ,使用下面的代码。

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Test {
    public static void main(String... args) {

    String str = "[{'id':1,'name':'yogesh'},{'id':2,'name':'aarush', 'degree': 'MCA'}]";

    Type type = new TypeToken<List<Map<Object, Object>>>() {
    }.getType();

    List<Map<Object, Object>> list = new Gson().fromJson(str, type);

    System.out.println(new Gson().toJson(list));
    filterList(list, "name", "yogesh");
    System.out.println(new Gson().toJson(list));

}

public static void filterList(List<Map<Object, Object>> list, String key, Object value) {
    for (Map<Object, Object> map : list) {
        if (map.containsKey(key)) {
            if (map.get(key).equals(value)) {
                list.remove(map);
            }
        }
    }
}
}

这里我过滤了 name=yogesh 记录。

输出:

[{"id":1.0,"name":"yogesh"},{"id":2.0,"name":"aarush","degree":"MCA"}]
[{"id":2.0,"name":"aarush","degree":"MCA"}]

关于java - 使用 java 中的 gson 库将过滤后的 JSON 数据从一个文件复制到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26052726/

相关文章:

java - Spring JSP : Trouble with spring form with modelAttribute ="" and path =""

python - 在 python 中解析 json 文件会造成困难

javascript - 如何使用 jsonp 请求获取 json

c# - 阅读 HTML?

algorithm - 学习 Bison : What are context-free grammars and LALR(1)?

ruby - 解析 ruby​​ 上的日志文件

java - 如何识别并构建 2007 年以来的 JBoss java 应用程序?

java - 从java设置xslt隐藏值

java - 如何将字节数组中的特定字节转换为整数

ruby-on-rails-3 - 比较两个嵌套的json对象rails 3