首先,约定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 ); } } }返回空值; }