一段字符串中间提取json字符串
function getJson(jsonStr) {
var stringStack = new stack();
var indexList = [];
var jsonList = [];
for (var i = 0; i < jsonStr.length; i++) {
if (jsonStr.charAt(i) == '{' || jsonStr.charAt(i) == '[') {
stringStack.push(new JsonStack(i, jsonStr.charAt(i)));
} else if (jsonStr.charAt(i) == '}' || jsonStr.charAt(i) == ']') {
if (stringStack.dataStore.length!=0) {
var js = stringStack.peek();
if (jsonStr.charAt(i) == '}' && js.char == '{') {
js = stringStack.pop();
} else if (jsonStr.charAt(i) == ']' && js.char == '[') {
js = stringStack.pop();
}
indexList.push(js.index);
indexList.push(i);
}
}
if (stringStack.dataStore.length==0 && indexList.length > 0) {
var tempStr = getJsonStr(indexList, jsonStr);
if (!(tempStr == null || tempStr.length == 0)) {
jsonList.push(tempStr);
}
indexList.splice(0,indexList.length);;
}
}
if (indexList != null && indexList.length > 0) {
var tempStr = getJsonStr(indexList, jsonStr);
if (!(tempStr == null || tempStr.length == 0)) {
jsonList.push(tempStr);
}
}
if (jsonList != null && jsonList.length > 0) {
return jsonList[0];
} else {
return null;
}
}
function getJsonStr(indexList, str) {
var temp = "";
for (var i = indexList.length - 1; i >= 0; i = i - 2) {
try {
temp = str.substring(indexList[i - 1], indexList[i] + 1);
JSON.parse(temp);
return temp;
} catch (e) {
continue;
}
}
return null;
}
function JsonStack(index, char) {
this.index = index;
this.char = char;
}
function stack() {
this.dataStore = [];//保存栈内元素,初始化为一个空数组
this.top = 0;//栈顶位置,初始化为0
this.push = push;//入栈
this.pop = pop;//出栈
this.peek = peek;//查看栈顶元素
this.clear = clear;//清空栈
this.length = length;//栈内存放元素的个数
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top - 1];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
package com.ucarinc.bizops.log;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* Created by zhangbo on 2017/12/29.
*/
public class FindJsonUtil {
public static List<String> format(String jsonStr) {
Stack<JsonStack> stringStack = new Stack<JsonStack>();
List<Integer> indexList = new LinkedList<Integer>();
List<String> jsonList = new ArrayList<String>();
for (int i = 0;i<jsonStr.length();i++) {
if(jsonStr.charAt(i)=='{'||jsonStr.charAt(i)=='['){
stringStack.push(new JsonStack(i,jsonStr.charAt(i)));
}else if(jsonStr.charAt(i)=='}'||jsonStr.charAt(i)==']'){
if(!stringStack.empty()){
JsonStack js = stringStack.peek();
if(jsonStr.charAt(i)=='}'&&js.getStr() =='{'){
js = stringStack.pop();
}else if(jsonStr.charAt(i)==']'&&js.getStr() =='['){
js = stringStack.pop();
}
indexList.add(js.getIndex());
indexList.add(i);
}
if(stringStack.empty()){
String tempStr= getJsonStr(indexList,jsonStr);
if(!(tempStr==null||tempStr.isEmpty())){
jsonList.add(tempStr);
}
indexList.clear();
}
}
}
if(indexList!=null && indexList.size()>0){
String tempStr= getJsonStr(indexList,jsonStr);
if(!(tempStr==null||tempStr.isEmpty())) {
jsonList.add(tempStr);
}
}
return jsonList;
}
private static String getJsonStr(List<Integer> indexList,String str) {
String temp= "";
for(int i = indexList.size() -1 ; i>=0 ; i=i-2){
try {
temp = str.substring(indexList.get(i - 1), indexList.get(i)+1);
JSON.parse(temp);
return str.substring(indexList.get(i - 1), indexList.get(i)+1);
}catch (Exception e){
continue;
}
}
return null;
}
static class JsonStack{
private Integer index;
private char str;
public JsonStack(Integer index, char str) {
this.index = index;
this.str = str;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public Character getStr() {
return str;
}
public void setStr(Character str) {
this.str = str;
}
}
}

一段字符串中间提取json字符串的更多相关文章
- jquery字符串数组转json字符串 C#json字符串转字符串list
一.jquery字符串数组转json字符串 var str=['1','2','3']; var jsonText= JSON.stringify(str);//把一个对象转换成json字符串 str ...
- Newtonsoft.Json解析json字符串和写json字符串
写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...
- C# 把对象序列化 JSON 字符串 和把JSON字符串还原为对象
/// <summary> /// 把对象序列化 JSON 字符串 /// </summary> /// <typeparam name="T"> ...
- 提取json字符串中指定格式中的参数值
直接上代码: import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; p ...
- python提取json字符串的值
json_str={ "actor":"邓超", "age":35, "book":[ "英语", ...
- [python]python子字符串的提取、字符串连接、字符串重复
1. python使用索引运算符[]和切片运算符[:],来提取字符串. 第一个字符的索引是0,最有一个字符的索引是-1,切片运算符[x:y]表示提取从索引x到索引y-1的字符,不包含索引y. 示例: ...
- 解析嵌套json字符串,一个json字符串中嵌套另一个json字符串
我现在有一个字符串是这样: { "msg": { ", "attrName": "sensorData", "trans ...
- 判断字符串是否为json字符串
public static class JsonSplitExtention { public static bool IsJson(this string json) { return JsonSp ...
- Newtonsoft.Json 操作 JSON 字符串
Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataContractJsonSeriali ...
随机推荐
- linux初级学习笔记三:linux操作系统及常用命令,及如何复制和移动文件!(视频序号:02_4)
本节学习的命令:cp,mv,install,du,read 本节学习的技能:文件的移动与复制 cp( copy):复制和移动文件 cp SRC DEST -r:递归复制一个目录及其目录中的所有文件 - ...
- 一步一步学Silverlight 2系列(7):全屏模式支持
一步一步学Silverlight 2系列(7):全屏模式支持 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...
- 【CQ18阶梯赛第二场】题解
[A-H国的身份证号码I] 用N个for语句可以搞定,但是写起来不方便,所以搜索. dfs(w,num,p)表示搜索完前w位,前面x组成的数位num,最后以为为p. 如果搜索到第N位,则表示num满足 ...
- 【ZJOI 2008】树的统计
[题目链接] 点击打开链接 [算法] 树链剖分模板题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 3000 ...
- liist不同遍历优缺点
JAVA中循环删除list中元素的方法总结 印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个 ...
- zabbix忘记登录密码重置方法
1.忘记zabbix之后,先用mysql查看一下: [root@centos7-106 ~]# mysql -uroot -p -e "select * from zabbix.users\ ...
- easyui 动态添加标签页,总结
步骤 1:创建 Tabs <div style="margin-bottom:10px"> <a href="#" class="e ...
- [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制
一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...
- class JsonItemExporter(BaseItemExporter):
class JsonItemExporter(BaseItemExporter):这个类的实现和几年前的实现有了点小变化,主要就是是否添加换行
- 洛谷 P1589 泥泞路
题目描述 暴雨过后,FJ的农场到镇上的公路上有一些泥泞路,他有若干块长度为L的木板可以铺在这些泥泞路上,问他至少需要多少块木板,才能把所有的泥泞路覆盖住. 输入输出格式 输入格式: 第一行为正整数n( ...