我正在尝试解析 JSON 数据。数据是一个数组,里面有对象。这是我从 URL 获得的 JSON 数组:
["{content:Airfare}",
"{content:Dues \/ Subscriptions}",
"{content:Education \/ Training}",
"{content:Entertainment}",
"{content:GS-OCWD}",
"{content:GS-OCWE}",
"{content:GS-Shift A}",
"{content:GS-Shift B}",
"{content:GS-Shift C}",
"{content:Ground Transportation}",
"{content:Legal Fees}",
"{content:Lodging}",
"{content:Meals}",
"{content:Mileage}",
"{content:Office Supplies}",
"{content:Other Expenses}",
"{content:Prof. Dues & Memberships}",
"{content:Rental Car}",
"{content:Telephone}",
"{content:Telephone \/ Internet}",
"{content:Tolls \/ Parking}"]
这是在我的 .m 文件中解析 JSON 数组的代码
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost:8080/de.vogella.jersey.final/rest/notes"]];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@",error);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", [item objectForKey:@"content"]);
[_identificationTypes1 addObject:item];
}
}
执行该行时NSLog(@"Item: %@", [item objectForKey:@"content"]);
,应用程序崩溃并给出[__NSCFString objectForKey:]: unrecognized selector
错误。它无法读取关键内容。如果我将行更改为,NSLog(@"Item: %@", item);
我可以看到所有值,例如{content:Airfare}
. 我只需要机票价值。有人能帮我吗
这是生成 JSON 的代码。我正在使用泽西岛和 JAVA。你能帮我处理 URL 中的 JSON 格式吗?这是我的 DAO 代码:
public JSONArray getAllNotes()
{
PreparedStatement prepStmt = null;
List<Note> notes = new ArrayList<Note>();
try {
String cSQL = "SELECT EXPENDITURE_TYPE FROM PA_ONLINE_EXPENDITURE_TYPES_V;
prepStmt = connection.prepareStatement(cSQL);
ResultSet result = prepStmt.executeQuery();
while (result.next())
{
Note note = new Note();
//note.setNoteId(result.getInt(1));
note.setContent(result.getString(1));
//note.setCreatedDate( new java.util.Date(result.getDate(3).getTime()));
notes.add(note);
}
return new JSONArray(notes);
} catch (SQLException e) {
e.printStackTrace();
prepStmt = null;
return null;
}
}
这是 POJO 方法:
@Override
public String toString() {
//return "{Content:"+content+"}" ;
return "ExpType [content=" + content + "]";
}
这是调用DAO方法的方法:
@GET
@Produces({MediaType.APPLICATION_JSON})
public JSONArray getNotes() {
return dao.getAllNotes();
}