Java iOS5:无法使用NSJSONSerialization从JSON读取值
我正在尝试解析JSON数据。数据是一个数组,其中包含对象。 这是我从URL获得的JSON数组:Java iOS5:无法使用NSJSONSerialization从JSON读取值,java,json,ios5,jersey,nsjsonserialization,Java,Json,Ios5,Jersey,Nsjsonserialization,我正在尝试解析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-Shif
["{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}"]
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;
}
}
这是在my.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];
}
}
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;
}
}
当行NSLog(@“Item:%@,[Item objectForKey:@“content”])时当应用程序崩溃并出现[\uu NSCFString objectForKey:]:无法识别的选择器时,将执行code>。它无法读取关键内容。如果我将行更改为NSLog(@“Item:%@”,Item)
我可以看到所有的值,比如{content:Airfare}
。我只需要机票的价格。有人能帮我吗
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;
}
}
这是生成JSON的代码。我正在使用Jersey和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;
}
}
这是调用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;
}
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public JSONArray getNotes() {
return dao.getAllNotes();
}
你的JSON是错误的。这只是一个字符串数组,这就是为什么会出现这个错误。它真正应该是这样的:
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;
}
}
[{"content":"Airfare"},
{"content":"Dues \/ Subscriptions"},
{"content":"Education \/ Training"},
... etc]
如何创建正确的格式?你能帮我一下吗我是新来的我想说的是问题在于你从你的url得到的回复。在您的例子中,您从http://localhost:8080/de.vogella.jersey.final/rest/notes
是罪魁祸首。您的objc代码没有问题。您能帮助我使用URL中的JSON格式吗?这是我的DAO代码。您是如何生成JSON的?它是服务器上的文件还是动态创建的?@user1342592对不起,伙计,JAVA不是我的事。我建议在你的问题中添加一个java标记,以吸引懂这种语言的人。