如何从Java文件中提取所需信息?

时间:2015-07-10 08:53:05

标签: java

以下是文件的内容:

  {"s_raw_message2":"something","CONNECTION_METHOD":"WMS","i_RXBufferLength":"2638","o":"1","DEVICE_OFFSET":"System=0#0;Application=0#0;Security=20150703100915.620080-000#984726;","s_ReporterHost":"ip","CONNECTION_MODE":"WMSapp","s_RV25":"FF98CA90-0394-1033-B130-001D92DD737D","i_TrustDeviceTime":"","s_RV24":"3642BCF0-0394-1033-8342-7C050726582F","s_RV23":"3642BCF0-0394-1033-8310-7C050726582F","s_ReporterPort":"49289","s_RV22":"6D6F80B1-02DF-1033-851D-001D92DD737D","s_Version":"2011.1r4-201409300209-release","s_RV21":"C76D2820-C395-1029-BB86-001321B5C0B3","s_RXBufferString":"something","s_ObserverHost":"ip","s_chainSequence":"34","s_sha256Hash":"dee6e845e41c65d5839198c7dd1052c5de7a53fcd885d3eb1d5491d2322c1a96","s_chainId":"1435917933352","s_ObserverIP":"ip"}
  {"s_raw_message2":"something","CONNECTION_METHOD":"WMS","i_RXBufferLength":"2638","o":"1","DEVICE_OFFSET":"System=0#0;Application=0#0;Security=20150703100915.620080-000#984726;","s_ReporterHost":"ip","CONNECTION_MODE":"WMSapp","s_RV25":"FF98CA90-0394-1033-B130-001D92DD737D","i_TrustDeviceTime":"","s_RV24":"3642BCF0-0394-1033-8342-7C050726582F","s_RV23":"3642BCF0-0394-1033-8310-7C050726582F","s_ReporterPort":"49289","s_RV22":"6D6F80B1-02DF-1033-851D-001D92DD737D","s_Version":"2011.1r4-201409300209-release","s_RV21":"C76D2820-C395-1029-BB86-001321B5C0B3","s_RXBufferString":"something","s_ObserverHost":"ip","s_chainSequence":"34","s_sha256Hash":"dee6e845e41c65d5839198c7dd1052c5de7a53fcd885d3eb1d5491d2322c1a96","s_chainId":"1435917933352","s_ObserverIP":"ip"}

此文件中有类似的条目,我想从此文件中提取信息,并以键值对的形式将其存储在另一个文件中,例如:

s_raw_message2 something
CONNECTION_METHOD WMS 
//and so on

请帮我写一个Java程序。

1 个答案:

答案 0 :(得分:1)

您的代码看起来像这个JSON文件,

{


"u1":{ "uname":"priya","age":22,"country":"India"},

"u2":{ "uname":"usha","age":22,"country":"India"},

"u3":{ "uname":"dharshini","age":22,"country":"India"},

"u4":{ "uname":"mom","age":22,"country":"India"},

"u5":{ "uname":"dad","age":22,"country":"India"},

"u6":{ "uname":"sis","age":22,"country":"India"},

"u7":{ "uname":"bro","age":22,"country":"India"}

}

您可以使用像我的代码

这样的ajax请求来解析此JSON文件
<script type="text/javascript">

function ajax_get_json()





{

    var hr = new XMLHttpRequest();

    hr.open("GET", "JSON/mylist.json", true);

    hr.setRequestHeader("Content-type", "application/json",true);

    hr.onreadystatechange = function() {

        if(hr.readyState == 4 && hr.status == 200) {

           /*  var return_data = hr.responseText; */

           var data=JSON.parse(hr.responseText);

           var status=document.getElementById("status");

           status.innerHTML = "";

           /* status.innerHTML=data.u1.country;  */

           for(var obj in data)

               {

               status.innerHTML+=data[obj].uname+" is in "+data[obj].country+"<br/>";
               }

        }
    }
    hr.send(null);

    status.innerHTML = "requesting...";
}
</script>
<body>
<div id="status"></div>
<script type="text/javascript">ajax_get_json();</script>
</body>
</html>