这是我的Json数据?如何解析android
如何解析它
{
"data":[
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
]}
答案 0 :(得分:0)
Google提供了一个非常好的json解析器GSON。 看看here
答案 1 :(得分:0)
Gson是一个极好的图书馆。首先为jsonschematopojo的json生成POJO。以下是POJO课程:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class JsonResponse {
@SerializedName("data")
@Expose
private List<Datum> data = null;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public class Datum {
@SerializedName("widget")
@Expose
private Widget widget;
public Widget getWidget() {
return widget;
}
public void setWidget(Widget widget) {
this.widget = widget;
}
}
public class Image {
@SerializedName("src")
@Expose
private String src;
@SerializedName("name")
@Expose
private String name;
@SerializedName("hOffset")
@Expose
private int hOffset;
@SerializedName("vOffset")
@Expose
private int vOffset;
@SerializedName("alignment")
@Expose
private String alignment;
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHOffset() {
return hOffset;
}
public void setHOffset(int hOffset) {
this.hOffset = hOffset;
}
public int getVOffset() {
return vOffset;
}
public void setVOffset(int vOffset) {
this.vOffset = vOffset;
}
public String getAlignment() {
return alignment;
}
public void setAlignment(String alignment) {
this.alignment = alignment;
}
}
public class Text {
@SerializedName("data")
@Expose
private String data;
@SerializedName("size")
@Expose
private int size;
@SerializedName("style")
@Expose
private String style;
@SerializedName("name")
@Expose
private String name;
@SerializedName("hOffset")
@Expose
private int hOffset;
@SerializedName("vOffset")
@Expose
private int vOffset;
@SerializedName("alignment")
@Expose
private String alignment;
@SerializedName("onMouseUp")
@Expose
private String onMouseUp;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHOffset() {
return hOffset;
}
public void setHOffset(int hOffset) {
this.hOffset = hOffset;
}
public int getVOffset() {
return vOffset;
}
public void setVOffset(int vOffset) {
this.vOffset = vOffset;
}
public String getAlignment() {
return alignment;
}
public void setAlignment(String alignment) {
this.alignment = alignment;
}
public String getOnMouseUp() {
return onMouseUp;
}
public void setOnMouseUp(String onMouseUp) {
this.onMouseUp = onMouseUp;
}
}
public class Widget {
@SerializedName("debug")
@Expose
private String debug;
@SerializedName("window")
@Expose
private Window window;
@SerializedName("image")
@Expose
private Image image;
@SerializedName("text")
@Expose
private Text text;
public String getDebug() {
return debug;
}
public void setDebug(String debug) {
this.debug = debug;
}
public Window getWindow() {
return window;
}
public void setWindow(Window window) {
this.window = window;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public Text getText() {
return text;
}
public void setText(Text text) {
this.text = text;
}
}
public class Window {
@SerializedName("title")
@Expose
private String title;
@SerializedName("name")
@Expose
private String name;
@SerializedName("width")
@Expose
private int width;
@SerializedName("height")
@Expose
private int height;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
}
现在复制整个json数据并将其粘贴到String变量中:
String jsonData="{\n" +
"\n" +
"\"data\":[ {\"widget\": { \"debug\": \"on\", \"window\": { \"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500 }, \"image\": { \"src\": \"Images/Sun.png\", \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\" }, \"text\": { \"data\": \"Click Here\", \"size\": 36, \"style\": \"bold\", \"name\": \"text1\", \"hOffset\": 250, \"vOffset\": 100, \"alignment\": \"center\", \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\" } }}\n" +
"\n" +
"]}"
Gson gson = new Gson();
JsonResponse jsonresp= gson.fromJson(jsonData, JsonResponse.class);
现在,如果您想从文件中读取,请应用以下代码:
public String mockDataReader(String fileName) {
try {
Path path = Paths.get(getClass().getClassLoader()
.getResource(fileName).toURI());
StringBuilder data = new StringBuilder();
Stream<String> lines = Files.lines(path);
lines.forEach((String line) -> data.append(line).append("\n"));
lines.close();
path = null;
return data.toString();
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}