问一个json数据结构问题 java的
{
"file":"flash_lz.png",
"frames":{
"cghb_jxyx":{
"x":1764,
"y":874,
"w":243,
"h":134,
"offX":0,
"offY":0,
"sourceW":243,
"sourceH":134
}
}
}
这是由什么类型嵌套组成
就是 基本的对象啊 ,没啥啊
你建个一模一样的javabean去解析就好了。。。
就是建javabean去解析试试
声明一个List对象,cghb_jxyx属性有:x":1764,"y",w","h","offX","offY","sourceW","sourceH",再声明一个对象,属性有file和List
冒号前面是属性名,冒号后面是属性值,按照属性名建一个JAVABean 接收一下就好了
声明一个list对象,创建一个file字段和一个类型为Map泛型为的类就可以
序列化与反序列化 就能实现,具体由什么生成的,这个得看由哪个反序列化成的
从Json格式上看是Map类型的,,这个Object分别是String类型的和Map或者是Map类型的,一般都是通过Json解析器转化而成。希望能帮到你。
从Json格式上看是
Map<String, Object>
类型的,,这个Object分别是String类型的和
Map<String, Integer>
或者是
Map<String, Object>
类型的,一般都是通过Json解析器转化而成。希望能帮到你。
Object{ Object{ Object{} } }
这个Object分别是String类型的和Map或者是Map类型的
这种键值对形式,使用map,如果还有第三级,map里面再嵌套map,javabean封装对象。
就是json对象嵌套json对象,如果使用gson解析的话,需要写三个类;
public class PhotoBean{
private String path;
private PhotoFrame frames;
}
public class PhotoFrame{
private PhotoData cghb_jxyx;
}
public class PhotoData{
private float x;
private float y;
private int w;
private int h;
private float offX;
private float offY;
private int sourceW;
private int sourceH;
}
//如果想直接解析的话,直接
try{
String data = new JSONObject(jsonStr).getJSONObject("frames").getJSONObject("cghb_jxyx").toString;
//需要什么就解析什么
}catch(JsonException e){
}
JsonObject中嵌套JsonObject,外面再套一层JsonObject JsonObject{JsonObject{JsonObject{}}}
用gson这个框架可以直接转成对象
JSONObject嵌套就可以,新建另个JSONObject, json1, jsion2 josn1.put("frames",json2)
JsonObject{JsonObject{JsonObject}}
可以把整个json理解成为一个对象,
该对象下面有file,frames两个属性,
file存的是一个普通属性,
frames是一个对象,存的同样是一个对象cghb_jxyx。
对象cghb_jxyx存了一系列的普通属性。
json 对象嵌套
這是地址坐標, MAP 類型集成的 Json. 上頭顯示的變量, 都是地球座標軸位置, 偏量.
package org.boking.classwork.bean;
/**
* json
* 1.可以封装成java bean 也就是简单的java对象,一般只包含属性和getter、setter方法
* 2.也可以封装成map
* 以上两点是对应 json 的最小元素来说,可以当然可以返回 List 和 数组等
*
* 但是json的键名要和javabean 的属性名一致,和map的键名一致
* map 封装和接送格式很相似,因为两个数据结构都是键值对应。只不过json最外层没有键名,而map 要有键名
*
* @author Boking
*
*/
public class Json {
private String file;
// 因为你的数字属性没有双引号,所以不是字符类型
private Frames frames;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public Frames getFrames() {
return frames;
}
public void setFrames(Frames frames) {
this.frames = frames;
}
}
class Frames {
private Cghb_jxyx cghb_jxyx;
public Cghb_jxyx getCghb_jxyx() {
return cghb_jxyx;
}
public void setCghb_jxyx(Cghb_jxyx cghb_jxyx) {
this.cghb_jxyx = cghb_jxyx;
}
}
class Cghb_jxyx {
private int x;
private int y;
private int w;
private int h;
private int offX;
private int offY;
private int sourceW;
private int sourceH;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public int getOffX() {
return offX;
}
public void setOffX(int offX) {
this.offX = offX;
}
public int getOffY() {
return offY;
}
public void setOffY(int offY) {
this.offY = offY;
}
public int getSourceW() {
return sourceW;
}
public void setSourceW(int sourceW) {
this.sourceW = sourceW;
}
public int getSourceH() {
return sourceH;
}
public void setSourceH(int sourceH) {
this.sourceH = sourceH;
}
}
基本模型{["",""]}
{
"":""
"":{["":""]}
"":{["":{"":"","":"","",""}]}
"":{["":{"":"","":"",{["":""]},""}]}
}
反正就是这样
整体对象用JSONArray接收
具体的对象用JSONObject对象接收
JSONArray arr = new JSONArray("");
JSONObject obj = arr.getObject("file");//可以直接获取到对应的值
嵌套的话就是多几层解析而已
package demo;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class App {
private String file;
private Map<String,Demo> frames;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public Map<String, Demo> getFrames() {
return frames;
}
public void setFrames(Map<String, Demo> frames) {
this.frames = frames;
}
@Override
public String toString() {
return "App{" +
"file='" + file + '\'' +
", frames=" + frames +
'}';
}
static class Demo {
private String x;
private String y;
private String w;
private String h;
private String offx;
private String offy;
private String sourceW;
private String sourceH;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public String getW() {
return w;
}
public void setW(String w) {
this.w = w;
}
public String getH() {
return h;
}
public void setH(String h) {
this.h = h;
}
public String getOffx() {
return offx;
}
public void setOffx(String offx) {
this.offx = offx;
}
public String getOffy() {
return offy;
}
public void setOffy(String offy) {
this.offy = offy;
}
public String getSourceW() {
return sourceW;
}
public void setSourceW(String sourceW) {
this.sourceW = sourceW;
}
public String getSourceH() {
return sourceH;
}
public void setSourceH(String sourceH) {
this.sourceH = sourceH;
}
public Demo(String x, String y, String w, String h, String offx, String offy, String sourceW, String sourceH) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.offx = offx;
this.offy = offy;
this.sourceW = sourceW;
this.sourceH = sourceH;
}
@Override
public String toString() {
return "Demo{" +
"x='" + x + '\'' +
", y='" + y + '\'' +
", w='" + w + '\'' +
", h='" + h + '\'' +
", offx='" + offx + '\'' +
", offy='" + offy + '\'' +
", sourceW='" + sourceW + '\'' +
", sourceH='" + sourceH + '\'' +
'}';
}
}
public static void main(String args[]) {
App a = new App();
Demo d = new Demo("1764", "874", "243", "134", "0", "0", "243", "134");
Map<String, Demo> paramMap = new HashMap<>();
paramMap.put("cghb_jxyx", d);
a.setFile("flash_lz.png");
a.setFrames(paramMap);
System.out.println(JSONObject.toJSONString(a));
}
}
输出结果:
{"file":"flash_lz.png","frames":{"cghb_jxyx":{"h":"134","offx":"0","offy":"0","sourceH":"134","sourceW":"243","w":"243","x":"1764","y":"874"}}}
Object{
Object{
Object{
}
}
}
该实体封装为 两个属性 一个是 private String file; private frames f(实体属性);
可以说是Object 的嵌套 如果你是开发Android 的话 一个bean 就可以搞定了
这个应该是 Object{String,Map{Object{}}}
"cghb_jxyx":{
"x":1764,
"y":874,
"w":243,
"h":134,
"offX":0,
"offY":0,
"sourceW":243,
"sourceH":134
}是frames对象的一个属性
file也是一个对象,和frames同级
var json = {"file":"flash_lz.png","frames":{"cghb_jxyx":{"x":1764,"y":874,"w":243,"h":134,"offX":0,"offY":0,"sourceW":243,"sourceH":134}}};
var file = json.file;
var frames = json.frames;
var cghb_jxyx = frames.cghb_jxyx;
var x = cghb_jxyx.x;
var y = cghb_jxyx.y;
........
就是基本的对象 只是多层嵌套而已 你可以一层一层的看
从Json格式上看是Map类型的,,这个Object分别是String类型的和Map或者是Map类型的,一般都是通过Json解析器转化而成。希望能帮到你。
List< Map < String,Object>>
@Test
public void jsonToMaps() {
String text = "{\n" + "\"file\":\"flash_lz.png\",\n" + "\"frames\":{\n" + "\"cghb_jxyx\":{\n" + "\"x\":1764,\n"
+ "\"y\":874,\n" + "\"w\":243,\n" + "\"h\":134,\n" + "\"offX\":0,\n" + "\"offY\":0,\n"
+ "\"sourceW\":243,\n" + "\"sourceH\":134\n" + "}}}";
Map map = JSONObject.parseObject(text);
diguiMap(map);
}
public void diguiMap(Map map) {
for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String value = JSON.toJSONString(entry.getValue());
System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
if (value.startsWith("{")) {
Map map1 = JSONObject.parseObject(value);
diguiMap(map1);
}
}
}
3层嵌套。每一层都可能是一个Map或者是实体类(Bean 或者说 Model)
你比如说:
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
map1.put("file", "flash_lz.png");
map1.put("frames", map2);
map2.put("cghb_jxyx", map3);
map3.put("x", 1764);
map3.put("y", 874);
后面的都添加到map3,当然每个map你都可以替换为一个实体类,属性名、属性值分别对应map的key、value,这应该好理解。
是一个对象,结构相当于是Map,
就是多层嵌套。对象里套了对象,再套对象。这个对象可以是 自己定义的对象乜嘢可以是map,都会产生这样的json
直接百度json在线解析,你会知道的
解答:全是对象嵌套构成
小技巧:“{}”是对象也就是Object构成,"[ ]"是集合
public class JsonRootBean {
private String file;
private Frames frames;
}
public class Frames {
private Cghb_jxyx cghb_jxyx;
}
public class Cghb_jxyx {
private int x;
private int y;
private int w;
private int h;
private int offX;
private int offY;
private int sourceW;
private int sourceH;
}
Map
键值对的嵌套。。 画不明白就用符号代替
最外层是两个对象 第二个对象中的值又是一个对象 然后这个对象的值有好多对象(属性 ,值 ) 总的来说,这是个封装了三层(?)的bean对象类似
大概就是这样一个数据
class Duang{
private String file;
private Frames frames;
class Frames{
private Cghb_jxyx cghb_jxyxl
}
class Cghb_jxyx{
private int x;
private int y;
private int w;
private int h;
private int offX;
private int offY;
private int sourceW;
private int sourceH;
}
}
所以就是这么一个货
Map嵌套可以实现,用Map作为对象属性
可以解析成:{String ,HashMap>},这个应该算简单的
最好下载一个json格式化工具,格式化一下json数据,仔细看一下数据结构有没有问题
Map>>