I am new to JSON.I am looking at the possibility of using GSON annotations in JSON file and getting the values in JSON file to a Java file.I tried this using "@serializedname" in JSON file but was unsuccesful.Below posted is my ".JSON" file and the ".java" files.
JSON file has got all the values and I'm trying to get those values into the .JAVA file
{
"valuess": [
{
"color": "red",
"value": "#f00"
},
{
"color": "green",
"value": "#0f0"
},
{
"color": "blue",
"value": "#00f"
},
{
"color": "cyan",
"value": "#0ff"
},
{
"color": "magenta",
"value": "#f0f"
},
{
"color": "yellow",
"value": "#ff0"
},
{
"color": "black",
"value": "#000"
}
]
}
This is the .Java file i have
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class file2
{
@SerializedName("valuess")
private List<valuess> vales;
public void setValuess(List<valuess>valuess)
{
this.vales = valuess;
}
public List<valuess> getVales()
{
return vales;
}
public String toString()
{
return "Values are"+vales;
}
}
Values.java
public class Values {
private String color;
private String value;
Values()
{
}
Values(String Color,String Value)
{
this.color=Color;
this.value = Value;
}
public void setColor(String Color)
{
this.color = Color;
}
public void setValue(String Value)
{
this.value = Value;
}
public String getColor()
{
return color;
}
public String getValue()
{
return value;
}
public String toString()
{
return "Colors[color="+color+"value="+value+"]";
}
}
This should be your File2 class code
public class File2
{
private Valuess[] valuess;
public Valuess[] getValuess ()
{
return valuess;
}
public void setValuess (Valuess[] valuess)
{
this.valuess = valuess;
}
@Override
public String toString()
{
return "ClassPojo [valuess = "+valuess+"]";
}
}
this should be your Valuess class code
public class Valuess
{
private String color;
private String value;
public String getColor ()
{
return color;
}
public void setColor (String color)
{
this.color = color;
}
public String getValue ()
{
return value;
}
public void setValue (String value)
{
this.value = value;
}
@Override
public String toString()
{
return "ClassPojo [color = "+color+", value = "+value+"]";
}
}
Now you can call the fromJson(String json, Class classOfT) method to convert the JSON to Object :
File2 file2 = new Gson.fromJson(your_json_string_here, File2.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.