Home >Backend Development >PHP Tutorial >怎么从JSON数据中取出相关数据

怎么从JSON数据中取出相关数据

PHPz
PHPzOriginal
2016-06-06 20:31:375811browse

从JSON数据中取出相关数据的方法:首先创建好JSON数据;然后使用方法【public void getData() throws Exception{}】获取相关json数据即可。

怎么从JSON数据中取出相关数据

怎么从JSON数据中取出相关数据?

JSON数据如下:

{
    "total": 1,
    "rows": [
        {
            "id": "1",
            "type": "1"
        },
        {
            "id": "2",
            "type": "2"
        },  
    ]
}

想要从上面的JSON数据中取出id,和type,可以使用如下方法

//获取待办信息环节
    public void getData() throws Exception{
       //获取到的json数据即如上面json数据
        String json = this.getJson();        
     JSONObject object=JSONObject.fromObject(json);
        JSONArray array = object.getJSONArray("rows");
        String id1 ="";  
     String type1 = "";
     String id2 ="";  
     String type2 = "";
        if(array.size()>1){
            Map map = (Map) array.get(0);
            id1 = (String)map.get("id");
       type1 = (String)map.get("type");
            id2 = (String)map.get("id");
       type2 = (String)map.get("type");
        }
    }

以上方法从上面json数据中取到了所需数据。

此方式可以从JSON数据中取到所需数据。

更多相关技术知识,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn