使用Java将String转换为Object Json Array

时间:2018-01-17 15:03:10

标签: java arrays json

我有这个字符串位于外部文件中。

 {
      "IsValid": true,
      "LiveSessionDataCollection": [
        {
          "CreateDate": "2017-12-27T13:29:06.595Z",
          "Data": "Khttp://www8.hp.com/us/en/large-format-printers/designjet-printers/products.html&AbSGOX+SGOXpLXpBF8CXpGOA9BFFPconsole.info('DeploymentConfigName%3DRelease_20171227%26Version%3D1')%3B&HoConfig: Release_20171227&AwDz//////8NuaCh63&Win32&SNgYAJBBYWCYKW9a&2&SGOX+SGOXpF/1en-us&AAAAAAAAAAAAQICBCXpGOAAMBBBB8jl",
          "DataFlags": 8,
          "DataFlagType": 264,
          "LegacyLiveSessionDataType": null,
          "LiveSessionId": 1545190526042650,
          "MessageNumber": 0,
          "StreamId": 0,
          "StreamMessageId": 0,
          "ProjectId": 201
        },
        {
          "CreateDate": "2017-12-27T13:29:08.887Z",
          "Data": "oDB Information Level : Detailed&9BbRoDB Annual Sales : 55000000&BoDB Audience : Mid-Market Business&AoDB%20Audience%20Segment%20%3A%20Retail%20%26%20Distribution&AoDB B2C : true&AoDB Company Name : Clicktale Inc&AoDB SID : 120325490&AoDB Employee Count : 275&AoDB Employee Range : Mid-Market&AoDB%20Industry%20%3A%20Retail%20%26%20Distribution&AoDB Revenue Range : $50M - $100M&AoDB Sub Industry : Electronics&AoDB Traffic : High&AWB9tY/8bvOBBP_({\"a\":[{\"a\":{\"s\":\"w:auto;l:auto;\"},\"n\":\"div53\"}]})&sP_({\"a\":[{\"a\":{\"s\":\"w:auto;l:auto;\"},\"n\":\"div62\"}]})&FP_({\"r\":[\"script2\"],\"m\":[{\"n\":{\"nt\":1,\"tn\":\"SCRIPT\",\"a\":{\"async\":\"\",\"src\":\"http://admin.brightcove.com/js/api/SmartPlayerAPI.js?_=1514381348598\"},\"i\":\"script55\"},\"t\":false,\"pn\":\"head1\"}]})&8GuP_({\"a\":[{\"a\":{\"s\":\"t:0px;mt:0px;l:274.5px;ml:0px;\"},\"n\":\"div442\"}]})&SP_({\"a\":[{\"a\":{\"s\":\"t:0px;mt:0px;l:274.5px;ml:0px;\"},\"n\":\"div444\"}]})&D",
          "DataFlags": 8,
          "DataFlagType": 264,
          "LegacyLiveSessionDataType": null,
          "LiveSessionId": 1545190526042650,
          "MessageNumber": 1,
          "StreamId": 0,
          "StreamMessageId": 1,
          "ProjectId": 201
        },
        {
          "CreateDate": "2017-12-27T13:29:08.971Z",
          "Data": "P_({\"a\":[{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div105\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div114\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div123\"}]})&9B+8P_({\"a\":[{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div167\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div169\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div178\"}]})&JP_({\"a\":[{\"a\":{\"s\":\"mih:457px;\"},\"n\":\"div220\"},{\"a\":{\"s\":\"mih:457px;\"},\"n\":\"div229\"},{\"a\":{\"s\":\"mih:457px;\"},\"n\":\"div238\"}]})&FP_({\"a\":[{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div282\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div291\"},{\"a\":{\"s\":\"mih:480px;\"},\"n\":\"div300\"}]})&HP_({\"a\":[{\"a\":{\"s\":\"t:0px;mt:-92px;l:274.5px;ml:0px;\"},\"n\":\"div442\"}]})&HP_({\"a\":[{\"a\":{\"s\":\"t:0px;mt:-92px;l:274.5px;ml:0px;\"},\"n\":\"div444\"}]})&B",
          "DataFlags": 8,
          "DataFlagType": 264,
          "LegacyLiveSessionDataType": null,
          "LiveSessionId": 1545190526042650,
          "MessageNumber": 2,
          "StreamId": 0,
          "StreamMessageId": 2,
          "ProjectId": 201
        },
        {
          "CreateDate": "2017-12-27T13:29:08.98Z",
          "Data": "P_({\"r\":[\"object1\",\"param1\",\"param2\",\"param3\",\"param4\",\"param5\",\"param6\",\"param7\",\"param8\",\"param9\",\"param10\",\"param11\",\"param12\",\"param13\",\"param14\",\"param15\"],\"m\":[{\"n\":{\"nt\":1,\"tn\":\"OBJECT\",\"a\":{\"type\":\"application/x-shockwave-flash\",\"i\":\"LNK--1710e8cd-4820-4be0-8cf0-28d57402afd8LNK--1710e8cd-4820-4be0-8cf0-28d57402afd8\",\"width\":\"720\",\"height\":\"422\",\"c\":\"BrightcoveExperience BrightcoveExperienceID_1039\",\"seamlesstabbing\":\"undefined\"},\"i\":\"object3\"},\"t\":false,\"pn\":\"div443\",\"ps\":\"meta29\"},{\"n\":{\"nt\":1,\"tn\":\"SCRIPT\",\"a\":{\"type\":\"text/javascript\",\"src\":\"http://admin.brightcove.com/js/api/SmartPlayerAPI.js\"},\"i\":\"script56\"},\"t\":false,\"pn\":\"div443\",\"ps\":\"object3\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"allowScriptAccess\",\"v\":\"always\"},\"i\":\"param31\"},\"t\":false,\"pn\":\"object3\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"allowFullScreen\",\"v\":\"true\"},\"i\":\"param32\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param31\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"seamlessTabbing\",\"v\":\"false\"},\"i\":\"param33\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param32\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"swliveconnect\",\"v\":\"true\"},\"i\":\"param34\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param33\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"wmode\",\"v\":\"opaque\"},\"i\":\"param35\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param34\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"quality\",\"v\":\"high\"},\"i\":\"param36\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param35\"},{\"n\":{\"nt\":1,\"tn\":\"PARAM\",\"a\":{\"name\":\"bgcolor\",\"v\":\"FFFFFF\"},\"i\":\"param37\"},\"t\":false,\"pn\":\"object3\",\"ps\":\"param36\"}]})&9CAQ",
          "DataFlags": 8,
          "DataFlagType": 264,
          "LegacyLiveSessionDataType": null,
          "LiveSessionId": 1545190526042650,
          "MessageNumber": 3,
          "StreamId": 0,
          "StreamMessageId": 3,
          "ProjectId": 201
        },
        {
          "CreateDate": "2017-12-27T13:29:09.413Z",
          "Data": "P_({\"a\":[{\"a\":{\"s\":\"w:720px;h:422px;p:relative;\"},\"n\":\"div443\"},{\"a\":{\"s\":\"p:relative;\"},\"n\":\"div445\"}],\"r\":[\"script55\"],\"m\":[{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"spooler\",\"s\":\"d:block;o:0;\"},\"i\":\"div451\"},\"t\":false,\"pn\":\"div443\"},{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"ispl_sm\",\"s\":\"o:1;\"},\"i\":\"div452\"},\"t\":false,\"pn\":\"div451\"},{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"layer\",\"s\":\"o:1;\"},\"i\":\"div453\"},\"t\":false,\"pn\":\"div451\",\"ps\":\"div452\"},{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"spooler\",\"s\":\"d:block;o:0;\"},\"i\":\"div454\"},\"t\":false,\"pn\":\"div445\"},{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"ispl_sm\",\"s\":\"o:1;\"},\"i\":\"div455\"},\"t\":false,\"pn\":\"div454\"},{\"n\":{\"nt\":1,\"tn\":\"DIV\",\"a\":{\"c\":\"layer\",\"s\":\"o:1;\"},\"i\":\"div456\"},\"t\":false,\"pn\":\"div454\",\"ps\":\"div455\"}]})&9CA5P_({\"a\":[{\"a\":{\"s\":\"d:block;o:0.0282439;\"},\"n\":\"div451\"},{\"a\":{\"s\":\"o:0.989022;\"},\"n\":\"div453\"},{\"a\":{\"s\":\"d:block;o:0.0282439;\"},\"n\":\"div454\"},{\"a\":{\"s\":\"o:0.989022;\"},\"n\":\"div456\"}]})&W",
          "DataFlags": 8,
          "DataFlagType": 264,
          "LegacyLiveSessionDataType": null,
          "LiveSessionId": 1545190526042650,
          "MessageNumber": 4,
          "StreamId": 0,
          "StreamMessageId": 4,
          "ProjectId": 201
        }
      ]

我正在尝试将其解析为JSON数组对象,当我在Google中搜索它时,我找到了以下解决方案:

JSONArray jsonArray = new JSONArray("path_to_file_to_parse");

但是当我在我的代码中写入它时,我收到了一个错误。还有另一种方法吗?

我正在使用json-simple version 1.1

1 个答案:

答案 0 :(得分:-1)

你看过Jackson Tree Model吗?

//first, you create a mapper object
ObjectMapper mapper = new ObjectMapper();

//then you create a JsonNode instance representing your JSON root structure
//you will need to define json yourself to run the code.
JsonNode root = null;
try {
    root = mapper.readTree(json);
} catch (IOException e) {
    System.out.println("Some Error");
}

//here you get the list of your session nodes
JsonNode list = root.path("LiveSessionDataCollection");

//then you can iterate through them and get any inner value
for (JsonNode session : list) {
    //for example, you can get the create date or live session id.
    System.out.println(session.path("CreateDate"));
    System.out.println(session.path("LiveSessionId"));
}

我可能不完全理解你的问题,但我认为这就是你所追求的。