字符串操作算法问题求助

别闹了费曼sir 2018-06-16 01:40:15
一个字符串操作的算法问题,想了好久,没做出来,大家能帮我看看吗?
字符串格式如下:
{"pwd":"1","b":"3","account":{"root":{"j":"5"}},"data":[{"127.0.0.1":{"root":"aa"},"127.0.0.2":{}}],"code":"200","msg":"成功"}
需要输出格式如下:
$.pwd
$.b
$.account
$.account['root']
$.account['root']['j']
$.data
$.data[0]['127.0.0.1']
$.data[0]['127.0.0.1']['root']
$.data[0]['127.0.0.2']
$.code
$.msg
要求:任意jsonObject和jsonArray的组合,不能用包,单纯的字符串操作,各路大神快来!
...全文
1075 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
stacksoverflow 2018-06-17
  • 打赏
  • 举报
回复
引用 3 楼 M201473392 的回复:
[quote=引用 2 楼 stacksoverflow 的回复:] 大概有这么三种 1.如果不嫌麻烦的话写一个parser 2.从左到右遍历字符串,左括号入栈,有括号出栈的方式,在遇到特殊符号时候,将想要的字符串从栈中取出的方式对json层级进行处理。 3.从左到右遍历字符串,针对题的规则(并不需要全部适应json规则,输出头的层级即可)暴力求解。 按第三种方式简单写了个大概的思路,自己改改(没处理中括号数组,没时间替你做题)。

import java.util.*;

public class JsonParser {

	public static void parse(String json) {
		
		LinkedList<String> currentNodeList = new LinkedList<String>();
		String currentNode = "$";
		StringBuffer currentStr = new StringBuffer();
		
		List<String> result = new ArrayList<String>();
		
		for (int i = 0; i < json.length(); i++) {
			char c = json.charAt(i);

			switch (c) {
				case '{':
						currentNodeList.add(currentNode);
					break;
				case '}':
					currentNodeList.removeLast();
					break;
				case ':':
					currentNode = getCurrentNodeStr(currentStr);
					currentStr = new StringBuffer();
					result.add(getParentStr(currentNodeList) + currentNode);
					break;
				default:
					currentStr.append(c);
					break;
			}
		}
		
		for(String node:result) {
			System.out.println(node);
		}
	}
	
	public static String getCurrentNodeStr(StringBuffer currentStr) {
		String temp = currentStr.substring(0, currentStr.lastIndexOf("\""));
		temp = temp.substring(temp.lastIndexOf("\"") + 1);
		return temp;
	}
	
	public static String getParentStr(List<String> parentList) {
		StringBuffer result = new StringBuffer();
		for(String node:parentList) {
			result.append(node);
			result.append(".");
		}
		
		return result.toString();
	}
	
	public static void main(String[] args) {
		
		String json = "{\"pwd\":\"1\",\"b\":\"3\",\"account\":{\"root\":{\"j\":\"5\"}},\"data\":[{\"127.0.0.1\":{\"root\":\"aa\"},\"127.0.0.2\":{}}],\"code\":\"200\",\"msg\":\"成功\"}";
		parse(json);
		
	}
}

感谢你给了个思路,但是你给的方法里面没有考虑到jsonArray的size大于1的情况,如果大于1输出就有变化,而且jsonArray和jsonObject还可以无限嵌套[/quote] 是的,我只是写了个开头,给你思路提供参考,自己照着改一改。
别闹了费曼sir 2018-06-17
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry 的回复:
主要就是解析json格式。用你的规则加json方法获取数据
不能用fastjson和gson之类的包
别闹了费曼sir 2018-06-17
  • 打赏
  • 举报
回复
引用 2 楼 stacksoverflow 的回复:
大概有这么三种 1.如果不嫌麻烦的话写一个parser 2.从左到右遍历字符串,左括号入栈,有括号出栈的方式,在遇到特殊符号时候,将想要的字符串从栈中取出的方式对json层级进行处理。 3.从左到右遍历字符串,针对题的规则(并不需要全部适应json规则,输出头的层级即可)暴力求解。 按第三种方式简单写了个大概的思路,自己改改(没处理中括号数组,没时间替你做题)。

import java.util.*;

public class JsonParser {

	public static void parse(String json) {
		
		LinkedList<String> currentNodeList = new LinkedList<String>();
		String currentNode = "$";
		StringBuffer currentStr = new StringBuffer();
		
		List<String> result = new ArrayList<String>();
		
		for (int i = 0; i < json.length(); i++) {
			char c = json.charAt(i);

			switch (c) {
				case '{':
						currentNodeList.add(currentNode);
					break;
				case '}':
					currentNodeList.removeLast();
					break;
				case ':':
					currentNode = getCurrentNodeStr(currentStr);
					currentStr = new StringBuffer();
					result.add(getParentStr(currentNodeList) + currentNode);
					break;
				default:
					currentStr.append(c);
					break;
			}
		}
		
		for(String node:result) {
			System.out.println(node);
		}
	}
	
	public static String getCurrentNodeStr(StringBuffer currentStr) {
		String temp = currentStr.substring(0, currentStr.lastIndexOf("\""));
		temp = temp.substring(temp.lastIndexOf("\"") + 1);
		return temp;
	}
	
	public static String getParentStr(List<String> parentList) {
		StringBuffer result = new StringBuffer();
		for(String node:parentList) {
			result.append(node);
			result.append(".");
		}
		
		return result.toString();
	}
	
	public static void main(String[] args) {
		
		String json = "{\"pwd\":\"1\",\"b\":\"3\",\"account\":{\"root\":{\"j\":\"5\"}},\"data\":[{\"127.0.0.1\":{\"root\":\"aa\"},\"127.0.0.2\":{}}],\"code\":\"200\",\"msg\":\"成功\"}";
		parse(json);
		
	}
}

感谢你给了个思路,但是你给的方法里面没有考虑到jsonArray的size大于1的情况,如果大于1输出就有变化,而且jsonArray和jsonObject还可以无限嵌套
stacksoverflow 2018-06-16
  • 打赏
  • 举报
回复
大概有这么三种 1.如果不嫌麻烦的话写一个parser 2.从左到右遍历字符串,左括号入栈,有括号出栈的方式,在遇到特殊符号时候,将想要的字符串从栈中取出的方式对json层级进行处理。 3.从左到右遍历字符串,针对题的规则(并不需要全部适应json规则,输出头的层级即可)暴力求解。 按第三种方式简单写了个大概的思路,自己改改(没处理中括号数组,没时间替你做题)。

import java.util.*;

public class JsonParser {

	public static void parse(String json) {
		
		LinkedList<String> currentNodeList = new LinkedList<String>();
		String currentNode = "$";
		StringBuffer currentStr = new StringBuffer();
		
		List<String> result = new ArrayList<String>();
		
		for (int i = 0; i < json.length(); i++) {
			char c = json.charAt(i);

			switch (c) {
				case '{':
						currentNodeList.add(currentNode);
					break;
				case '}':
					currentNodeList.removeLast();
					break;
				case ':':
					currentNode = getCurrentNodeStr(currentStr);
					currentStr = new StringBuffer();
					result.add(getParentStr(currentNodeList) + currentNode);
					break;
				default:
					currentStr.append(c);
					break;
			}
		}
		
		for(String node:result) {
			System.out.println(node);
		}
	}
	
	public static String getCurrentNodeStr(StringBuffer currentStr) {
		String temp = currentStr.substring(0, currentStr.lastIndexOf("\""));
		temp = temp.substring(temp.lastIndexOf("\"") + 1);
		return temp;
	}
	
	public static String getParentStr(List<String> parentList) {
		StringBuffer result = new StringBuffer();
		for(String node:parentList) {
			result.append(node);
			result.append(".");
		}
		
		return result.toString();
	}
	
	public static void main(String[] args) {
		
		String json = "{\"pwd\":\"1\",\"b\":\"3\",\"account\":{\"root\":{\"j\":\"5\"}},\"data\":[{\"127.0.0.1\":{\"root\":\"aa\"},\"127.0.0.2\":{}}],\"code\":\"200\",\"msg\":\"成功\"}";
		parse(json);
		
	}
}

oyljerry 2018-06-16
  • 打赏
  • 举报
回复
主要就是解析json格式。用你的规则加json方法获取数据

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧