2015-02-07 66 views
0

我有以下代码:选择JSON对象取决于值

[ { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 0 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 1 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 2 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 3 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 4 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 5 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 6 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 7 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 8 
}, { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 9 
} ] 

现在,通过生成的文本我想要搜索的价值:

try { 
      List<Item> object = new ArrayList<Item>(); 
      for (int i = 0; i < 10; i++) { 
       Item item = new Item(); 
       item.setItemID(i); 
       object.add(item); 
       item=null; 
      }   
      ObjectMapper objectMapper = new ObjectMapper(); 
      objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS); 
      ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter(); 
      String json = ow.writeValueAsString(object); 
      System.out.println(json); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

通过sysout我喜欢控制台得到的字符串每个对象的属性。

例如: 我想搜索itemID=6,如果找到,那么需要选择整个对象。所以我的结果文本将是

[ { 
    "dateCreated" : null, 
    "createdBy" : null, 
    "dateModified" : null, 
    "modifiedBy" : null, 
    "itemID" : 6 
} ] 

我想在java中的解决方案不在jquery或任何客户端脚本。

回答

0

“result”是一个包含你的json的变量。下面的代码是用java编写的。

JSONObject jObj = new JSONObject(result); 
JSONArray jArr = jObj.getJSONArray("array"); 
jArr.get(6); 

jArr.get(6)将在6指数

而在具体的值返回对象是这样的:

JSONArray jArr = jObj.getJSONArray("array"); 
    for (int i = 0; i < jArr.length(); i++) { 
    JSONObject jobj1 = jArr.getJSONObject(i); 
     if(jobj1.getInt("itemID")==6){ 
     //Do your work } 
    } 
+1

OP说'我想搜索每个对象属性的值。'如果他想用'createdBy'搜索,那么怎么办?按索引搜索不是一个解决方案,除非他只想通过id搜索 - 他不知道。 – gudthing 2015-02-07 12:52:11

+0

@gudthing说的是正确的。按itemid搜索仅仅是一个例子。我可以搜索任何财产。 – Amogh 2015-02-07 12:59:34

+0

你可以运行一个for循环到json的大小,并且在每个索引处你可以使用你的密钥来匹配item ID。像这样 \t JSONArray jArr = jObj.getJSONArray(“array”); \t \t对(INT I = 0; I 2015-02-07 13:02:39

0

所以,你有Item对象的名单。这Item有一个像dateCreated会,createdBy,属性...

假设你有这些属性的干将:

如果你要搜索的属性,你遍历所有的List

for (int i = 0; i < list.size(); i++) { 
if (list.get(i).getDateCreated().equals(_Date-you-are-looking-for)) { 
    return list.get(i); 
} 
} 

在另一方面,如果你要搜索的JSON字符串导致,我会用正则表达式来捕捉整个元素你想要的:

/\{.*?itemID=6.*?\}/