我在java中有这样的东西
Object dym=[{Auto Phrasing=false,
New Term=buy,
Number of Results=21,
Spell Correction=true}];
' Object '将始终显示这4个属性(这些值来自Json响应)。我想从那里拉出“新术语”值。我该怎么办?
答案 0 :(得分:2)
我建议将数组封装到类中,如下所示:
public class Something {
public boolean autoPhrasing;
public String newTerm;
public int numResults;
public boolean spellCorrection;
// Constructor
Something(boolean phrasing, String term, int results, boolean correct) {
autoPhrasing = phrasing;
newTerm = term;
numResults = results;
spellCorrection = correct;
}
}
然后,您可以为遇到的每个JSON对象创建一个新的Something
对象。
要从中获取值,您只需要从对象中调用该字段,如下所示:
Something foo = new Something(false, "buy", 21, true);
boolean isPhrasing = foo.autoPhrasing;
String term = foo.newTerm;
int results = foo.numResults;
boolean isCorrecting = foo.spellCorrection;
答案 1 :(得分:2)
使用像Jackson这样的Java JSon框架。它可以将JSON结构映射到Java对象。