0

I am trying to parse JSON data. The data is an Array with objects inside it. This is the JSON array I get from the URL:

["{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}"]

This is the code for parsing the JSON array in my .m file

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];
        }

    }

When the line NSLog(@"Item: %@", [item objectForKey:@"content"]); is executed the app crashes and gives a [__NSCFString objectForKey:]: unrecognized selector error. It is unable to read the key content. If I change the line to NSLog(@"Item: %@", item); I can see all the values like {content:Airfare}. I just need the Airfare value. Can someone help me

This is the code to generate the JSON. I am using Jersey and JAVA. Can you help me with the JSON format from the URL? This is my DAO code:

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;
        }
    }                       

This is the POJO method:

 @Override
        public String toString() {
            //return "{Content:"+content+"}" ;
            return "ExpType [content=" + content +  "]";
        }      

This is the method that calls the DAO method:

@GET

 @Produces({MediaType.APPLICATION_JSON})
        public JSONArray getNotes() {
            return dao.getAllNotes();
        }     
user1342592
  • 49
  • 1
  • 12

1 Answers1

0

Your JSON is wrong. It's just an array of strings and that's why you're getting this error. What it really should be like is:

[{"content":"Airfare"},
{"content":"Dues \/ Subscriptions"},
{"content":"Education \/ Training"},
... etc]
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • How do you create the correct format? Can you please help me I am new to this – user1342592 Jun 26 '12 at 15:06
  • What I'm trying to say is that the problem is on the response you get from your url. In your case the format of the JSON you're getting from `http://localhost:8080/de.vogella.jersey.final/rest/notes` is the culprit. There is not something wrong with your objc code. – Alladinian Jun 26 '12 at 15:09
  • Can you help me with the JSON format from the URL? This is my DAO code – user1342592 Jun 26 '12 at 15:21
  • How are you producing the JSON? Is it a file on your server or you create it dynamically? – Alladinian Jun 26 '12 at 15:27
  • @user1342592 Sorry mate, JAVA is not my thing. I would suggest to add a java tag into your question to attract people that know the language. – Alladinian Jun 26 '12 at 15:33
  • I managed to get the correct JSON format but the app still crashes on objectForKey.. Any suggestions? – user1342592 Jun 26 '12 at 17:41