2015-02-05 62 views
-4

的一部分,我有這樣的字符串:Java的隔離繩

[{uuid=80461af7-f6c7-448d-8aea-a5fd07bd4cfc, folderId=92711, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:32:19 GMT 2015, modifiedDate=Thu Feb 05 13:54:46 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92711/, **name=Cyber**, description=, lastPostDate=Wed Feb 04 02:32:19 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}, {uuid=51d3719c-d787-49c1-a565-bd40e6515154, folderId=92715, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:32:43 GMT 2015, modifiedDate=Wed Feb 04 02:32:43 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92715/, **name=Operational**, description=, lastPostDate=Thu Feb 05 05:04:43 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}, {uuid=06bf396f-7814-431d-8e3d-2488a757d970, folderId=92707, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:31:57 GMT 2015, modifiedDate=Thu Feb 05 13:54:21 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92707/, **name=SAS**, description=, lastPostDate=Wed Feb 04 02:31:57 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}]

我需要通過像本例中的字符串,而只提取使用Java名稱,輸入每個名稱爲陣列。在這個例子中,我只需要Cyber​​,Operational,SAS,每個輸入一個數組。

謝謝

+3

這看起來像格式錯誤的JSON ...非常畸形的JSON。即使傑克遜也無能爲力...... – fge 2015-02-05 20:23:01

+0

你可能想看看正則表達式。即使格式錯誤的JSON,你也應該能夠提取你需要的東西。 – 2015-02-05 20:24:08

+0

我一直在嘗試使用正則表達式,但是我不知道如何捕獲「name =」和昏迷之間的單詞,以及多個實例。 – 2015-02-05 21:42:12

回答

0

請問下面的東西是否適合您?這提取了Cyber​​,Operational和SAS。

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 
import java.util.ArrayList; 
import java.util.List; 




public class HelloWorld{ 


public static void main(String []args){ 
    List matches = new ArrayList(); 


    Pattern p = Pattern.compile("\\*\\*name=(.*?)\\*\\*"); 

    String data = "[{uuid=80461af7-f6c7-448d-8aea-a5fd07bd4cfc, folderId=92711, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:32:19 GMT 2015, modifiedDate=Thu Feb 05 13:54:46 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92711/, **name=Cyber**, description=, lastPostDate=Wed Feb 04 02:32:19 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}, {uuid=51d3719c-d787-49c1-a565-bd40e6515154, folderId=92715, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:32:43 GMT 2015, modifiedDate=Wed Feb 04 02:32:43 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92715/, **name=Operational**, description=, lastPostDate=Thu Feb 05 05:04:43 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}, {uuid=06bf396f-7814-431d-8e3d-2488a757d970, folderId=92707, groupId=10202, companyId=10154, userId=10198, userName=Test Test, createDate=Wed Feb 04 02:31:57 GMT 2015, modifiedDate=Thu Feb 05 13:54:21 GMT 2015, repositoryId=10202, mountPoint=false, parentFolderId=92703, treePath=/92703/92707/, **name=SAS**, description=, lastPostDate=Wed Feb 04 02:31:57 GMT 2015, defaultFileEntryTypeId=0, hidden=false, overrideFileEntryTypes=false, status=0, statusByUserId=0, statusByUserName=, statusDate=null}]" ; 

    Matcher m = p.matcher(data); 
    int index = 0; 
    while(m.find(index)) { 
     matches.add(m.group(1)); 
     System.out.println(m.group(1)); 
     index = m.end(); 
    } 

} 
} 

查找()尋找開始於索引(initally 0),基團()的匹配返回無論是在之間()在正則表達式,並將其添加到陣列。最後,比賽結束被設置爲新的起始索引。這循環,直到找到所有匹配

+0

非常感謝,但名稱= Cyber​​的任何一方都是**,是一個意外,我用粗體字來說明我需要抓住什麼,並且它在兩側都添加了**。我需要它在「name =」和「,」之間攫取 – 2015-02-05 22:23:59

+0

將正則表達式更改爲'name =(。*?),訣竅? – Hedge7707 2015-02-05 22:33:38

+0

是的,非常感謝你是最棒的! – 2015-02-05 23:35:45