关于用java语言解析json字符串,获取相关字段。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于用java语言解析json字符串,获取相关字段。相关的知识,希望对你有一定的参考价值。

string json={"请求地址:":"http://218.4.136.115:9999/TPFrameM/rest/mobilecertservice/bindusercert","请求参数:":{"head{"timestamp":1574297981146,"token":"A3205820001@kArXrdlJpdhSm_koadbCRefeBcg=@MTU3NDI5ODI4MQ=="},"params":{"devicenum":"3c4027c1f8e43be9d70e58a81f4888ff","unitorgnum":"11111118-1","enccert":"MIICBzCCAXKgAwIBAgIDAcmoMAsGCSqGSIb3DQEBBTAmMQswCQYDVQQGEwJDTjEXMBUGA1UEAwwOQ0NJVCBTVUJDQSBSU0EwHhcNMTkwMTI1MDYwMDU1WhcNMTkxMjI1MDYwMDU1WjAgMQswCQYDVQQGEwJDTjERMA8GA1UEAwwIY3BjZXNoaTMwgZ0wCwYJKoZIhvcNAQEBA4GNADCBiQKBgQDI+ydFwY5sK0OrDdWsh/cSDbUPkse3esx3GK+0mBOlT2DUPcK5Y556WYWRLLVMAjCn+sjWaOPxeflzhZOxy4u/yGX1/sypA46dQIN0aD1jei0S0hY3gVJrPfrx3zodUNsPb6dqVAnwmQCnQKSp2lcgH1yxcWjQz3p39tGXwOMhPwIDAQABo08wTTAfBgNVHSMEGDAWgBQYGFNuXDC5rQKfOVjF6EoILc2nIDALBgNVHQ8EBAMCBDAwHQYDVR0OBBYEFKrUXUW7PMsjZ/CCXWaa0g19GFnsMAsGCSqGSIb3DQEBBQOBgQDLDVJCDbemhAnuDw9xCXR96lwBbS1rlXSUGUtDTDlSx0r9FNWRqI+2/FOymmixQffrJxc/Yq/BVjs+bOYp5zbP1vlFgKZLeSGw+iflcjK9h0z6bjVVQSv0M+6RohF3MT4MvxCQ4b33eNSXJghcxetxKR5n6POgQ51yVTLV0DT4kA==","signcert":"MIICizCCAfagAwIBAgIDAcmnMAsGCSqGSIb3DQEBBTAmMQswCQYDVQQGEwJDTjEXMBUGA1UEAwwOQ0NJVCBTVUJDQSBSU0EwHhcNMTkwMTI1MDYwMDU1WhcNMTkxMjI1MDYwMDU1WjAgMQswCQYDVQQGEwJDTjERMA8GA1UEAwwIY3BjZXNoaTMwggEgMAsGCSqGSIb3DQEBAQOCAQ8AMIIBCgKCAQEAvKYwCoX1H2u4XnoQLlVJMcxD3vgj6y29VcjsRwLWtjfglE1tOPfjhOY0XzGSq/o9js5BaaJ86+BB0c+SGXr8CPyz+Xvw0q0VPM1k89QlA8AZKwgRqrewCzgQcIjWXmjcQTi+/KA2MWCj79spSh5RrG9bxOUEgu+rlZIVl9G0c8E412Iq6STxQhUI3BRSkiC8OjhyvmjbiEFSeybyrLCSZl25IlJ2sXy1O0ph+v2OrZqsbsNgStm1UzBPCCQHhpJpK6+5bYdEOBrIexBAmlJHO8S/Vhv/I8Xj/G7nu/xdIHWQSJwVdff1os4GGSfbwpYDljX9WyikIuWWOoymTbmm9QIDAQABo08wTTAfBgNVHSMEGDAWgBQYGFNuXDC5rQKfOVjF6EoILc2nIDALBgNVHQ8EBAMCBsAwHQYDVR0OBBYEFDec42nFEyxcHetNci3RwwL71DYEMAsGCSqGSIb3DQEBBQOBgQCaobse+1LYH+AvLzQ+EaJwRFeagKojt0xTk5hErK0XfBhFLo3DYkaffOIvZutzZOUMbj1JegCo7HKRRoulrQuV5cizGu9dpYZHiVLoSiEWev0yxoruoxCGvlUsn4ZworI3Pd4KINvnmwxxw0QtzJVeYYAQoAAaKORfQJc7ZCpK3w==","idcard":"320723199504111045","displayname":"123","mobile":"18012895810","usertype":"2","dognum":"01c9a7","admindanweiname":"cpceshi3","token":"A3205820001@kArXrdlJpdhSm_koadbCRefeBcg=@MTU3NDI5ODI4MQ==","adminmobile":""}},"返回值:":{"custom":"","status":{"code":"0","text":"管理员授权证书申请失败,的证书对应的单位名称cpceshi3与交易系统不一致"}}}

首先在该字符串两侧加上"[]":json = "[" + json + "]";

JSONArray jsonArray = JSONArray.fromObject(json);//转化为json数组。该方法必须以“[”开头,"]"结尾。

jsonArray.getJSONObject(0);//获取索引为0json数组相当于集合中的第一个。getJSONObject()方法用来获取内部还包含子字段json数组。

jsonArray.getJSONObject(0).getJSONObject("返回值:").getJSONObject("status").get("text").toString();//当没有子字段时调用get(key)方法获取字段对应的属性。

以上是关于关于用java语言解析json字符串,获取相关字段。的主要内容,如果未能解决你的问题,请参考以下文章

我有一个json数组怎么获取某个字段的对应的对象

java多种方式解析json字符串

关于Json数据动态解析的处理方法

java遍历复杂json字符串获取想要的数据

java解析Json中获取Array字段值及嵌套Json对象

怎么用程序解析一个json文件