package bean;
import comgooglegsonannotationsSerializedName;
import javautilList;
public class TestBean
{
private String status;
private Msg msg;
public String getStatus() {
return status;
}
public void setStatus(String status) {
thisstatus = status;
}
public Msg getMsg() {
return msg;
}
public void setMsg(Msg msg) {
thismsg = msg;
}
public class Msg
{
private List<City> city;
public List<City> getCity() {
return city;
}
public void setCity(List<City> city) {
thiscity = city;
}
public class City
{
@SerializedName("@attributes")
private SpecialKey key;//奇怪的key处理
public SpecialKey getKey() {
return key;
}
public void setKey(SpecialKey key) {
thiskey = 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) {
thiscityX = cityX;
}
public String getCityY() {
return cityY;
}
public void setCityY(String cityY) {
thiscityY = cityY;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
thiscityname = cityname;
}
public String getCentername() {
return centername;
}
public void setCentername(String centername) {
thiscentername = centername;
}
public String getFontColor() {
return fontColor;
}
public void setFontColor(String fontColor) {
thisfontColor = fontColor;
}
public String getPyName() {
return pyName;
}
public void setPyName(String pyName) {
thispyName = pyName;
}
public String getState1() {
return state1;
}
public void setState1(String state1) {
thisstate1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
thisstate2 = state2;
}
public String getStateDetailed() {
return stateDetailed;
}
public void setStateDetailed(String stateDetailed) {
thisstateDetailed = stateDetailed;
}
public String getTem1() {
return tem1;
}
public void setTem1(String tem1) {
thistem1 = tem1;
}
public String getTem2() {
return tem2;
}
public void setTem2(String tem2) {
thistem2 = tem2;
}
public String getTemLow() {
return temLow;
}
public void setTemLow(String temLow) {
thistemLow = temLow;
}
public String getWindState() {
return windState;
}
public void setWindState(String windState) {
thiswindState = windState;
}
public String getWindDir() {
return windDir;
}
public void setWindDir(String windDir) {
thiswindDir = windDir;
}
public String getWindPower() {
return windPower;
}
public void setWindPower(String windPower) {
thiswindPower = windPower;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
thishumidity = humidity;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
thisurl = url;
}
}
}
}
}
可以使用Gson,没有可以去网上下载一个。注意bean文件的对应的类型可以改成你自己数据的类型,其实如果没有算数运算全用String最好,免得返回奇怪的东西报错。关键在于用@SerializedName("@attributes")处理奇怪的key
TestBean bean = new Gson()fromJson(json, TestBeanclass);
List<TestBeanMsgCity> cityList = beangetMsg()getCity();
for (int i = 0; i < cityListsize();i++)
{
if (cityListget(i)getKey()getCityname()equals("福州"))
{
// 这就是你要的那条“福州”的对象了
}
}
其实这样分隔,要不要考虑2个字符串之间除了逗号之外的空白呢?
public class Test {
public static void main(String[] args) {
String str = "zifuchun1,zifuchuan2,zifuchuan3,zifuchuan4";
String[] ary = strsplit(",");//调用API方法按照逗号分隔字符串
for(String item: ary){
Systemoutprintln(item);
}
}
}
----------------
zifuchun1
zifuchuan2
zifuchuan3
zifuchuan4
String str = "\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"";
String regex = "\"[^\"]+[\"\\,\\s]+([^\"]+)";
String result = strreplaceAll(regex, "$1");
Systemoutprintln(result);
importjavaawt;importjavaapplet;importjavaawtevent;publicclassUseTextEventextendsAppletimplementsActionListener,TextListener{TextFieldt1;TextAreat2;Panelp1;publicvoidinit(){t1=newTextField(25);t2=newTextArea(10,25);t1addActionListener(this);t1addTextListener(this);p1=newPanel(newBorderLayout());p1add(t1,BorderLayoutNORTH);p1add(t2,BorderLayoutSOUTH);add(p1);}publicvoidtextValueChanged(TextEventt){if(tgetSource()==t1){t1setText(t1getText());t1setCaretPosition(t1getText()length());//设置输入后的长度t2setText(t1getText());}}publicvoidactionPerformed(ActionEvente){if(egetSource()==t1)t2setText("");}}这个可以使光标放在文字的后面,但是光标还是会闪动,不稳定。
import javautilregexMatcher;
import javautilregexPattern;
public class RecE {
Pattern pattern;
Matcher matcher;
/ ps:
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
预定义字符类
任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
/
public void getMatchedPattern(String regx,String source){
pattern = Patterncompile(regx);
matcher = patternmatcher(source);
int index = 1;
while(!matcherhitEnd()){
if(matcherfind()){
Systemoutprintln("找到第"+index +"个"+matchergroup());
index++;
}
}
Systemoutprintln("一共找到"+(index-1)+"组匹配项");
}
public static void main(String[] args) {
new RecE()getMatchedPattern("love","ilove12what i love23e tolove dolove a efe");
}
}
结果:
找到第2个love
找到第3个love
找到第4个love
一共找到4组匹配项
int a[]={0,0,0,1,2,3,0,0};
int i=0;
while(a[i]==0){ //找到最后一个0
i++;
}
Systemoutprintln(i);
int j=0;
int b[]= new int[alength-i]; //用来储存新数组
for(;i<alength;i++){
b[j]=a[i];
Systemoutprint(b[j]);
j++;
}
以上就是关于java中怎么将json数据中的一段提取出来全部的内容,包括:java中怎么将json数据中的一段提取出来、java中字符串怎样截取成一个一个的小段、java字符串截取字符串中间一段等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)