java中怎么将json数据中的一段提取出来?
发布网友
发布时间:2022-04-25 07:16
我来回答
共2个回答
热心网友
时间:2023-01-23 16:04
package bean;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class TestBean
{
private String status;
private Msg msg;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Msg getMsg() {
return msg;
}
public void setMsg(Msg msg) {
this.msg = msg;
}
public class Msg
{
private List<City> city;
public List<City> getCity() {
return city;
}
public void setCity(List<City> city) {
this.city = city;
}
public class City
{
@SerializedName("@attributes")
private SpecialKey key;//奇怪的key处理
public SpecialKey getKey() {
return key;
}
public void setKey(SpecialKey key) {
this.key = key;
}
public class SpecialKey
{
private String cityX;
private String cityY;
private String cityname;
private String centername;
private String fontColor;
private String pyName;
private String state1;
private String state2;
private String stateDetailed;
private String tem1;
private String tem2;
private String temLow;
private String windState;
private String windDir;
private String windPower;
private String humidity;
private String url;
public String getCityX() {
return cityX;
}
public void setCityX(String cityX) {
this.cityX = cityX;
}
public String getCityY() {
return cityY;
}
public void setCityY(String cityY) {
this.cityY = cityY;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getCentername() {
return centername;
}
public void setCentername(String centername) {
this.centername = centername;
}
public String getFontColor() {
return fontColor;
}
public void setFontColor(String fontColor) {
this.fontColor = fontColor;
}
public String getPyName() {
return pyName;
}
public void setPyName(String pyName) {
this.pyName = pyName;
}
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
this.state2 = state2;
}
public String getStateDetailed() {
return stateDetailed;
}
public void setStateDetailed(String stateDetailed) {
this.stateDetailed = stateDetailed;
}
public String getTem1() {
return tem1;
}
public void setTem1(String tem1) {
this.tem1 = tem1;
}
public String getTem2() {
return tem2;
}
public void setTem2(String tem2) {
this.tem2 = tem2;
}
public String getTemLow() {
return temLow;
}
public void setTemLow(String temLow) {
this.temLow = temLow;
}
public String getWindState() {
return windState;
}
public void setWindState(String windState) {
this.windState = windState;
}
public String getWindDir() {
return windDir;
}
public void setWindDir(String windDir) {
this.windDir = windDir;
}
public String getWindPower() {
return windPower;
}
public void setWindPower(String windPower) {
this.windPower = windPower;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
}
}
可以使用Gson,没有可以去网上下载一个。注意bean文件的对应的类型可以改成你自己数据的类型,其实如果没有算数运算全用String最好,免得返回奇怪的东西报错。关键在于用@SerializedName("@attributes")处理奇怪的key
TestBean bean = new Gson().fromJson(json, TestBean.class);
List<TestBean.Msg.City> cityList = bean.getMsg().getCity();
for (int i = 0; i < cityList.size();i++)
{
if (cityList.get(i).getKey().getCityname().equals("福州"))
{
// 这就是你要的那条“福州”的对象了
}
}
热心网友
时间:2023-01-23 17:22
import com.alibaba.fastjson.JSONObject;
public class JsonTest {
public static void main(String[] args) {
// json串(以自己的为准)
String str = "{"id":"75","shoppingCartItemList":[{"id":"407","num":"10"}]}";
JSONObject jsonObject = JSONObject.parseObject(str);
// 获取到key为shoppingCartItemList的值
String r = jsonObject.getString("shoppingCartItemList");
System.out.println(r);
}
}