【Android】解析.json格式文件

本文介绍了解析特定格式的JSON文件的方法,包括如何读取文件、解析JSON数据结构及获取具体字段值的过程。通过实例展示了如何操作JSON数组和布尔类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解析.json格式文件

{
	"version":"1.1",
	"parts":{
		"cheek":{
			"zPosition":2, 
			"width":220,
			"height":102,
			"triggerStop":false,
            "scale":{			
				"scaleX":{		
					"pointA":[{"x":66.0,"y":8.0,"index":74}],
					"pointB":[{"x":154.0,"y":5.0,"index":77}]
				},
				"scaleY":{		
					"pointA":[],
					"pointB":[]
				}
			},	
			"position":{		
				"positionX":[{"x":66.0,"y":8.0,"index":74},{"x":154.0,"y":5.0,"index":77}],		
				"positionY":[]		
			},
			"rotateCenter":[
			 	{"x":110.0,"y":68.0,"index":46}
			]
		}
    }
}


 private String readFileFromAsset(String fileName){


        BufferedReader reader = null;
        InputStream inputStream = null;
        StringBuilder builder = new StringBuilder();

        try{
            inputStream = getAssets().open(fileName);
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;

            while((line = reader.readLine()) != null)
            {
                builder.append(line);
                builder.append("\n");
            }
        } catch (IOException ioe){
            ioe.printStackTrace();
        } finally {

            if(inputStream != null)
            {
                try {
                    inputStream.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }

            if(reader != null)
            {
                try {
                    reader.close();
                } catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        }
        return builder.toString();
    }

带中括号的为数组;

Boolean型的使用:booleanValue()


解析:

JSON products = new JSON(readFileFromAsset("rabbit.json"));
String  version = products.key("version").stringValue();
JSON rabbit_part = products.key("parts").key("cheek");
boolean bb = rabbit_part.key("triggerStop").booleanValue();
String a = rabbit_part.key("scale").key("scaleX").key("pointA").stringValue();
Log.e("json", "===" + a + "===");
float b = rabbit_part.key("scale").key("scaleX").key("pointA").index(0).key("x").doubleValue().floatValue();
Log.e("json", "===" + b + "===");






评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值