Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象上的JSON数组到字符串列表_Java_Json_Gson - Fatal编程技术网

Java 对象上的JSON数组到字符串列表

Java 对象上的JSON数组到字符串列表,java,json,gson,Java,Json,Gson,我有下面的JSON [{ "rowId": "03 EUR10580000" }, { "rowId": "03 EUR10900001" }, { "rowId": "03 EUR1053RUD*" }, { "rowId": "033331" }] 我想把它转换成一个只包含rowId值的字符串列表,在本例中 "03 EUR10580000" "03 EUR10900001" "03 EUR1053RUD*" "033331" 我使用Gson fromJso

我有下面的JSON

[{
    "rowId": "03 EUR10580000"
}, {
    "rowId": "03 EUR10900001"
}, {
    "rowId": "03 EUR1053RUD*"
}, {
    "rowId": "033331"
}]
我想把它转换成一个只包含rowId值的字符串列表,在本例中

"03 EUR10580000"
"03 EUR10900001"
"03 EUR1053RUD*"
"033331"

我使用Gson fromJson实现了这一点,但作为回报,我得到了一个LinkedTreeMap列表,当我在中执行循环时失败。我想要一个简单的字符串列表。

嗯,您的字符串不是“字符串列表”的json。它包含对象列表。因此,您可以创建一个以rowID作为字符串属性的类

类数据

  • rowID(类型字符串)
然后,您可以使用Gson解析这个JSON字符串,以列出所使用的


或者您必须手动准备一个新的json解析器。

嗯,您的字符串不是“字符串列表”的json。它包含对象列表。因此,您可以创建一个以rowID作为字符串属性的类

类数据

  • rowID(类型字符串)
然后,您可以使用Gson解析这个JSON字符串,以列出所使用的

或者您必须手动准备一个新的json解析器。

编写一个POJO类,如下所示:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RootObject {

    @SerializedName("rowId")
    @Expose
    private String rowId;

    public String getRowId() {
        return rowId;
    }

    public void setRowId(String rowId) {
        this.rowId = rowId;
    }

}
然后只需创建
列表
并从POJO获取值。

编写一个POJO类,如下所示

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RootObject {

    @SerializedName("rowId")
    @Expose
    private String rowId;

    public String getRowId() {
        return rowId;
    }

    public void setRowId(String rowId) {
        this.rowId = rowId;
    }

}

然后只需创建
List
并从POJO获取值。

如果您只想使用
Gson
快速解析字符串,只需创建生成器并使用“default”
List
Map
实例来表示Java中的
JSON
。如果您想要更安全的类型,或者您想要在“更大”的项目中使用解析的实例,我建议您创建一个POJO,如其他答案中所述

final GsonBuilder gsonBuilder = new GsonBuilder();

// you may want to configure the builder

final Gson gson = gsonBuilder.create();

/*
 * The following only works if you are sure that your JSON looks
 * as described, otherwise List<Object> may be better and a validation,
 * within the iteration.
 */
@SuppressWarnings("unchecked") 
final List<Map<String, String>> list = gson.fromJson("[{ ... }]", List.class);

final List<String> stringList = list.stream()
            .map(m -> m.get("rowId"))
            .collect(Collectors.toList());

System.out.println(stringList);
final GsonBuilder GsonBuilder=new GsonBuilder();
//您可能需要配置生成器
final Gson Gson=gsonBuilder.create();
/*
*只有当您确信JSON看起来
*如前所述,否则列表可能更好,并进行验证,
*在迭代中。
*/
@抑制警告(“未选中”)
final List=gson.fromJson(“[{…}]”,List.class);
最终列表stringList=List.stream()
.map(m->m.get(“rowId”))
.collect(Collectors.toList());
System.out.println(stringList);

如果您只想使用
Gson
快速解析字符串,您只需创建生成器,并使用“默认”
列表和
Map
实例来表示Java中的
JSON
。如果您想要更安全的类型,或者您想要在“更大”的项目中使用解析的实例,我建议您创建一个POJO,如其他答案中所述

final GsonBuilder gsonBuilder = new GsonBuilder();

// you may want to configure the builder

final Gson gson = gsonBuilder.create();

/*
 * The following only works if you are sure that your JSON looks
 * as described, otherwise List<Object> may be better and a validation,
 * within the iteration.
 */
@SuppressWarnings("unchecked") 
final List<Map<String, String>> list = gson.fromJson("[{ ... }]", List.class);

final List<String> stringList = list.stream()
            .map(m -> m.get("rowId"))
            .collect(Collectors.toList());

System.out.println(stringList);
final GsonBuilder GsonBuilder=new GsonBuilder();
//您可能需要配置生成器
final Gson Gson=gsonBuilder.create();
/*
*只有当您确信JSON看起来
*如前所述,否则列表可能更好,并进行验证,
*在迭代中。
*/
@抑制警告(“未选中”)
final List=gson.fromJson(“[{…}]”,List.class);
最终列表stringList=List.stream()
.map(m->m.get(“rowId”))
.collect(Collectors.toList());
System.out.println(stringList);
您有:

String str = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]"
您可以这样做(不需要任何像gson这样的外部库):

str=str.replace(“{”rowId\“:\”,”).replace(“\”,”).replace(“[”,”).replace(“]),”);
列表rowIDs=str.split(“,”);
如果字符串格式为JSON,您还可以
trim()
rowIDs中的每个字符串

您有:

String str = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]"
您可以这样做(不需要任何像gson这样的外部库):

str=str.replace(“{”rowId\“:\”,”).replace(“\”,”).replace(“[”,”).replace(“]),”);
列表rowIDs=str.split(“,”);

如果字符串的格式是JSON,您还可以
trim()
rowIDs中的每个字符串

您需要将JSON字符串解析为
JsonArray
。然后迭代
JsonArray
实例,将每个json元素添加到列表
ls
。以下代码sinppet是解决方案:

    List<String> ls = new ArrayList<String>();
    String json = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]";
    JsonArray ja = new JsonParser().parse(json).getAsJsonArray();

    for(int i = 0; i < ja.size(); i++) {
       ls.add(ja.get(i).getAsJsonObject().get("rowId").toString());
    }
    for(String rowId : ls) {
       System.out.println(rowId);
    }
    /* output : 
    "03 EUR10580000"
    "03 EUR10900001"
    "03 EUR1053RUD*"
    "033331" */
List ls=new ArrayList();
字符串json=“[{\'rowId\':\'03 EUR10580000\',{\'rowId\':\'03 EUR10900001\',{\'rowId\':\'03 EUR1053RUD*\”,{\'rowId\':\'033331\'”;
JsonArray ja=new-JsonParser().parse(json.getAsJsonArray();
对于(int i=0;i
您需要将json字符串解析为
JsonArray
。然后迭代
JsonArray
实例,将每个json元素添加到列表
ls
。以下代码sinppet是解决方案:

    List<String> ls = new ArrayList<String>();
    String json = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]";
    JsonArray ja = new JsonParser().parse(json).getAsJsonArray();

    for(int i = 0; i < ja.size(); i++) {
       ls.add(ja.get(i).getAsJsonObject().get("rowId").toString());
    }
    for(String rowId : ls) {
       System.out.println(rowId);
    }
    /* output : 
    "03 EUR10580000"
    "03 EUR10900001"
    "03 EUR1053RUD*"
    "033331" */
List ls=new ArrayList();
字符串json=“[{\'rowId\':\'03 EUR10580000\',{\'rowId\':\'03 EUR10900001\',{\'rowId\':\'03 EUR1053RUD*\”,{\'rowId\':\'033331\'”;
JsonArray ja=new-JsonParser().parse(json.getAsJsonArray();
对于(int i=0;i
什么失败了?你能告诉我们你在迭代中使用的代码吗?您可以轻松地使用
LinkedTreeMap
values()
方法,该方法返回一个
集合。或者您可以迭代这些条目,将它们映射到值并收集它们(使用,例如,lamba)。对于杰克逊来说,这真的很简单,我相信对于Gson来说也是如此。什么失败了?你能告诉我们你在迭代中使用的代码吗?您可以轻松地使用
LinkedTreeMap
values()
方法,该方法返回一个
集合。或者,您可以对条目进行迭代