双层嵌套json字符串(即json对象内嵌json数组)解析为Map

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。
之前我层写过一篇文章,介绍了json与map的相互转化,但当时只涉及到单一的json对象或json数组,对json对象内嵌套这json数组的json字符串无法处理,这篇文章主要解决这个问题。
之前的那篇文章址:http://blog.csdn.net/u012116457/article/details/24371877
首先要在项目中导入json的jar包:
这里写图片描述
在下面的代码中处理json对象既使用了net.sf.json.JSONObject 也使用了org.json.JSONObject 两个的包都要导。

首先在E盘下创建一个priceJson.txt,写入一下内容:

{
	"height":1,
	"width":1,
	"location":[
	           {
	           "顶部":"3"
	           },{
	           "底部":"1"
	           },{
	           "左侧":"2"
	           },{
	           "右侧":"1"
	           },{
	           "悬浮":"4"
	           }
	],
	"type":[
	         {
	           "1":"1"
	           },{
	           "2":"2"
	           },{
	           "3":"4"
	           },{
	           "4":"4"
	           }
	]
}

下面的类会通过read方法将文件中的json串读取出来,通过getMapByJson获取到map:

package com.ngsh.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.json.JSONArray;

public class FileIO {
	
	//读文件
	public String read(String path){
		String data = "";
		File file = new File(path);
		if(file.isFile()&&file.exists()){
			try {
				InputStreamReader read = new InputStreamReader(
				    new FileInputStream(file),"utf-8");//考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
			    while((lineTxt = bufferedReader.readLine()) != null){
                    data +=lineTxt;
			    }
                read.close();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return data;

	}
	//将json转换为map
	public Map<String, Object> getMapByJson(String json) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 最外层解析
		JSONObject object = object = JSONObject.fromObject(json);
		for (Object k : object.keySet()) {
			Object v = object.get(k);
			map.put(k.toString(), v);
		}
		Map<String, Object> map2 = new HashMap<String, Object>();
		//第二层解析 第二层可能是 也可能不是
		for(Map.Entry<String, Object> entry:map.entrySet()){
			try {
				JSONArray array = new JSONArray(entry.getValue().toString());  //判断是否是json数组
				
				//是json数组
				for (int i = 0; i < array.length(); i++) {
					org.json.JSONObject object2 = array.getJSONObject(i);//json数组对象
					JSONObject object3 = JSONObject.fromObject(object2.toString());  //json对象
					for (Object k : object3.keySet()) {
						Object v = object3.get(k);
						map2.put(k.toString(), v);
					}
				}
			} catch (Exception e) {  //不是json串数组
				map2.put(entry.getKey(), entry.getValue());
			}
		}
	/*	for(Map.Entry<String, Object> entry:map2.entrySet()){
			System.out.println(entry.getKey()+"-"+entry.getValue());
		}
	*/
		return map2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 String path="E:\\priceJson.txt";
		 FileIO fo = new FileIO();
         Map map = fo.getMapByJson(fo.read(path));
         for(Map.Entry<String, Object> entry:map.entrySet()){
 			System.out.println("key:"+entry.getKey()+"-value:"+entry.getValue());
 		}
	}

}

运行结果如下:

key:3-value:4
key:2-value:2
key:1-value:1
key:height-value:1
key:左侧-value:2
key:4-value:4
key:width-value:1
key:底部-value:1
key:悬浮-value:4
key:右侧-value:1
key:顶部-value:3
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光光-Leo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值