如何使用java和Gson将json字符串转换为json数组
我有一个json字符串,里面有一个数组,我只想获取数组部分 JSON字符串:如何使用java和Gson将json字符串转换为json数组,java,json,gson,Java,Json,Gson,我有一个json字符串,里面有一个数组,我只想获取数组部分 JSON字符串: { "jsonKeywords": [ { "xmlNodeKWId": 0, "xmlNodeId": 35, "nodeKeyword": "dfdfdf", "keywordPosition": "Top", "keywordPrefix": "",
{
"jsonKeywords": [
{
"xmlNodeKWId": 0,
"xmlNodeId": 35,
"nodeKeyword": "dfdfdf",
"keywordPosition": "Top",
"keywordPrefix": "",
"keywordSuffix": "abcd",
"valueInLine": 0,
"keywordMatchPattern": "",
"templateId": 3
}
]
}
我只想将其转换为:
[
{
"xmlNodeKWId": 0,
"xmlNodeId": 35,
"nodeKeyword": "dfdfdf",
"keywordPosition": "Top",
"keywordPrefix": "",
"keywordSuffix": "abcd",
"valueInLine": 0,
"keywordMatchPattern": "",
"templateId": 3
}
]
我正在使用Gson库来处理JSON。
可以这样做吗。响应类:
public class Response{
@SerializedName("jsonKeywords")
private List<JsonKeywordsItem> jsonKeywords;
public void setJsonKeywords(List<JsonKeywordsItem> jsonKeywords){
this.jsonKeywords = jsonKeywords;
}
public List<JsonKeywordsItem> getJsonKeywords(){
return jsonKeywords;
}
@Override
public String toString(){
return
"Response{" +
"jsonKeywords = '" + jsonKeywords + '\'' +
"}";
}
}
公共类响应{
@SerializedName(“jsonKeywords”)
私有列表关键字;
public void setJsonKeywords(列出jsonKeywords){
this.jsonKeywords=jsonKeywords;
}
公共列表getJsonKeywords(){
返回jsonKeywords;
}
@凌驾
公共字符串toString(){
回来
“答复{”+
“jsonKeywords='”+jsonKeywords+'\''+
"}";
}
}
JsonKeywordsItem类:
public class JsonKeywordsItem {
@SerializedName("keywordSuffix")
private String keywordSuffix;
@SerializedName("valueInLine")
private int valueInLine;
@SerializedName("keywordMatchPattern")
private String keywordMatchPattern;
@SerializedName("nodeKeyword")
private String nodeKeyword;
@SerializedName("keywordPrefix")
private String keywordPrefix;
@SerializedName("keywordPosition")
private String keywordPosition;
@SerializedName("templateId")
private int templateId;
@SerializedName("xmlNodeKWId")
private int xmlNodeKWId;
@SerializedName("xmlNodeId")
private int xmlNodeId;
public void setKeywordSuffix(String keywordSuffix) {
this.keywordSuffix = keywordSuffix;
}
public String getKeywordSuffix() {
return keywordSuffix;
}
public void setValueInLine(int valueInLine) {
this.valueInLine = valueInLine;
}
public int getValueInLine() {
return valueInLine;
}
public void setKeywordMatchPattern(String keywordMatchPattern) {
this.keywordMatchPattern = keywordMatchPattern;
}
public String getKeywordMatchPattern() {
return keywordMatchPattern;
}
public void setNodeKeyword(String nodeKeyword) {
this.nodeKeyword = nodeKeyword;
}
public String getNodeKeyword() {
return nodeKeyword;
}
public void setKeywordPrefix(String keywordPrefix) {
this.keywordPrefix = keywordPrefix;
}
public String getKeywordPrefix() {
return keywordPrefix;
}
public void setKeywordPosition(String keywordPosition) {
this.keywordPosition = keywordPosition;
}
public String getKeywordPosition() {
return keywordPosition;
}
public void setTemplateId(int templateId) {
this.templateId = templateId;
}
public int getTemplateId() {
return templateId;
}
public void setXmlNodeKWId(int xmlNodeKWId) {
this.xmlNodeKWId = xmlNodeKWId;
}
public int getXmlNodeKWId() {
return xmlNodeKWId;
}
public void setXmlNodeId(int xmlNodeId) {
this.xmlNodeId = xmlNodeId;
}
public int getXmlNodeId() {
return xmlNodeId;
}
@Override
public String toString() {
return
"JsonKeywordsItem{" +
"keywordSuffix = '" + keywordSuffix + '\'' +
",valueInLine = '" + valueInLine + '\'' +
",keywordMatchPattern = '" + keywordMatchPattern + '\'' +
",nodeKeyword = '" + nodeKeyword + '\'' +
",keywordPrefix = '" + keywordPrefix + '\'' +
",keywordPosition = '" + keywordPosition + '\'' +
",templateId = '" + templateId + '\'' +
",xmlNodeKWId = '" + xmlNodeKWId + '\'' +
",xmlNodeId = '" + xmlNodeId + '\'' +
"}";
}
}
Gson gson = new Gson();
Response response = gson.fromJson("Your response string", Response.class);
List<JsonKeywordsItem> mList = response.getJsonKeywords();
公共类JsonKeywordsItem{
@SerializedName(“关键字后缀”)
私有字符串关键字后缀;
@SerializedName(“valueInLine”)
私人机构;
@SerializedName(“关键字匹配模式”)
私有字符串关键字匹配模式;
@序列化名称(“nodeKeyword”)
私有字符串nodeKeyword;
@SerializedName(“关键字前缀”)
私有字符串关键字前缀;
@序列化名称(“关键字位置”)
私有字符串关键字位置;
@SerializedName(“模板ID”)
私有int-templateId;
@SerializedName(“xmlNodeKWId”)
私有int xmlNodeKWId;
@SerializedName(“xmlNodeId”)
私有int-xmlNodeId;
public void setKeywordSuffix(字符串关键字后缀){
this.keywordSuffix=关键字后缀;
}
公共字符串getKeywordSuffix(){
返回关键字后缀;
}
public void setValueInLine(int valueInLine){
this.valueInLine=valueInLine;
}
public int getValueInLine(){
返回值内联;
}
public void setKeywordMatchPattern(字符串关键字匹配模式){
this.keywordMatchPattern=关键字匹配模式;
}
公共字符串getKeywordMatchPattern(){
返回关键字匹配模式;
}
公共void setNodeKeyword(字符串nodeKeyword){
this.nodeKeyword=nodeKeyword;
}
公共字符串getNodeKeyword(){
返回nodeKeyword;
}
public void setKeywordPrefix(字符串关键字前缀){
this.keywordPrefix=关键字前缀;
}
公共字符串getKeywordPrefix(){
返回关键字前缀;
}
public void setKeywordPosition(字符串关键字位置){
this.keywordPosition=关键字位置;
}
公共字符串getKeywordPosition(){
返回关键字位置;
}
公共void setTemplateId(int templateId){
this.templateId=templateId;
}
public int getTemplateId(){
返回模板ID;
}
公共无效集合xmlNodeKWId(int xmlNodeKWId){
this.xmlNodeKWId=xmlNodeKWId;
}
public int getXmlNodeKWId(){
返回xmlNodeKWId;
}
公共无效setXmlNodeId(int-xmlNodeId){
this.xmlNodeId=xmlNodeId;
}
public int getXmlNodeId(){
返回xmlNodeId;
}
@凌驾
公共字符串toString(){
回来
“JsonKeywordsItem{”+
“关键字后缀='”+关键字后缀+'\''+
“,valueInLine='”+valueInLine+'\''+
,keywordMatchPattern='“+keywordMatchPattern+'\'”+
“,nodeKeyword='”+nodeKeyword+'\''+
“,keywordPrefix='”+keywordPrefix+'\''+
,keywordPosition='“+keywordPosition+'\'”+
“,templateId=”“+templateId+”\“””+
“,xmlNodeKWId=”+xmlNodeKWId+'\''+
“,xmlNodeId='”+xmlNodeId+'\”+
"}";
}
}
Gson Gson=新的Gson();
Response=gson.fromJson(“您的响应字符串”,Response.class);
List mList=response.getJsonKeywords();
快速的方法是使用Map
和未选中的强制转换来提取jsonKeywords
元素。假设将JSON读入inputJson
变量:
Gson gson = new Gson();
Map<String, ?> map = gson.fromJson(inputJson, Map.class);
System.out.println(gson.toJson(map.get("jsonKeywords")));
您必须使用JSONARRAY从JSON获取数据数组:
JSONObject jsonObject= new JSONObject(jsonResponse);
JSONArray jsonArray = jsonObject.getJSONArray("jsonKeywords");
同时,@B001ᛦ 这样的评论既不友好也没有帮助。一个单一的链接到一个类似的问题将不胜感激…谁能给我一个链接?在那里我可以找到这个或评论什么写在谷歌o搜索。有一个
JSONObject jsonObject= new JSONObject(jsonResponse);
JSONArray jsonArray = jsonObject.getJSONArray("jsonKeywords");