首页 > 编程知识 正文

正则提取json字段,json怎么解析数据

时间:2023-05-03 14:21:26 阅读:163674 作者:3444

首先,约定json表示节点的规则。 例如

{

' data': {

' req': [{

' node1': 'value1'表示,

' node2': 'value2'

、{

“node3': 'value3”为,

' node4': 'value4'

() ) ]

}

}

获取node2的值时,规则必须填写JSON.data.req[0].node2

获取node3的值时,规则必须填写JSON.data.req[1].node3

分析并获取具体值的方法如下所示

/** *解析节点* * @ param obj * @ param node * @ return */privatestaticjsonobjectgetobj (jsonobjectobj,stringnode(try ) itry ) i arr.size (; I () if ) (I ' ) (.equals ) node.substring (node.indexof (,replace ) ) )、node.index of (,replace ) ) ) ) ) ) ) ) ) ) ) ) }catch(exceptione ) {return obj; }返回空值; } /** *节点值* @ paramjsoncontent * @ paramjsonpath * @ return * @ throws exception */publicstaticsynchronizedstringetngetnodetodet String jsonPath ) throws exception { string [ ] nodes=JSON path jsonobjectobj=JSON object.parse object (JSON content; for(intI=1; i nodes.length; I ) if(obj!=null(obj=getobj(obj,nodes[i] ) ); (if ) ) I1 )==nodes.length ) try ) returnobj.getstring ) nodes[I]; }catch(exceptione ) return ' JSON exception : ' e.getmessage ),NodeString:' obj.toString ); } } }返回空值; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。