我有以下JSON-String数组
[
{
"firstName": "Patrick",
"lastName": "Smith",
"loginName":"test0003@test.com",
"Country":"US",
"phoneNumber": "287 125-1434",
"status": "340"
},
{
"firstName": "Bob",
"lastName": "Williams",
"loginName":"test0002@test.com",
"Country":"US",
"phoneNumber": "213 111-9943",
"status": "215"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "340"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "215"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "167"
},
{
"firstName": "George",
"lastName": "Jones",
"loginName":"test0004@test.com",
"Country":"FR",
"phoneNumber": "217 987-2634",
"status": "340"
}
]
我只需返回此部分
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "167"
}
为 loginName 和 status
提供值我需要创建一个如下所示的方法。
public String filterJsonArray(String array, String keyOne, Object valueOne, String keyTwo, Object valueTwo) throws ParseException {
}
有人可以告诉我如何实现这个目标。
答案 0 :(得分:0)
使用杰克逊,这是我能想到的最粗略的片段:
#include <stdio.h>
int main (int argc, char **argv) {
for (;;) {
char buf;
fread(&buf, 1, 1, stdin);
if ('q' == buf)
break;
fwrite(&buf, 1, 1, stdout);
fflush(stdout);
}
return 0;
}
当然它只会返回给定对的第一场比赛。如果您需要所有值,请改为返回列表并在循环内填充它。
答案 1 :(得分:0)
我使用JSONObject,这是另一种解析方法。
1.使用JSONArray
解析
2.循环数组并读入UserProfile对象
3.将其存储在HashMap中以使用密钥
import java.util.HashMap;
import java.util.Map;
import org.json.*;
class UserProfile{
String loginName;
int status;
String firstName;
String key;
UserProfile(){
}
String getLoginName(){
return loginName;
}
String getFirstName(){
return firstName;
}
void setKey(String key){
this.key = key;
}
void setLoginName(String loginName){
this.loginName = loginName;
}
void setStatus(int status){
this.status = status;
}
void setFirstName(String firstName){
this.firstName = firstName;
}
}
public class JSONObjParser {
public static void main(String[] args){
Map<String, UserProfile> map = new HashMap<String, UserProfile>();
String msg ="[{ firstName: Patrick, lastName: Smith, loginName:test0003@test.com, Country:US, phoneNumber: 287 125-1434, status: 340 }, { firstName: Bob, lastName: Williams, loginName:test0002@test.com, Country:US, phoneNumber: 213 111-9943, status: 215 }]";
JSONArray jsonarray = new JSONArray(msg);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String loginName = jsonobject.getString("loginName");
int status = jsonobject.getInt("status");
String firstName = jsonobject.getString("firstName");
UserProfile profile = new UserProfile();
profile.setFirstName(firstName);
profile.setLoginName(loginName);
profile.setStatus(status);
String key = loginName + Integer.toString(status);
map.put(key, profile);
}
for (String key : map.keySet()) {
UserProfile profile = map.get(key);
System.out.println("Key = " + key + ", FirstName = " + profile.getFirstName());
}
}
}