1

I have two java classes for parsing jason into java alone. Beyond that, the classes are not used for any thing. Below are the two classes.

import java.util.ArrayList;

public class PaymentsPaid {

    public ArrayList<PaidDetailAmounts> amount;
}

and

public class PaidDetailAmounts {

    public Long invoiceFeeId;

    public Double amountPaid;
}

Here is where the string and the use of and object mapper.

 "amount": [{"invoiceFeeId": 12085, "amountPaid": 100},{"invoiceFeeId": 12084, "amountPaid": 100},{"invoiceFeeId": 12086, "amountPaid": 500}]

and the mapper code

ObjectMapper mapper = new ObjectMapper();        
try {
    PaymentsPaid paymentsPaidModel = mapper.readValue(httpServletRequest.getParameter("amount"), PaymentsPaid.class);
   /*
    Iterator<PaidDetailAmounts> iterator = paymentsPaidModel.amount.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next().invoiceFeeId);
    }
    */
} catch (JsonParseException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

This is my exception:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field " invoiceFeeId" (Class PACKAGE_NAME.PaymentsPaid), not marked as ignorable

I must be doing something worng, because I built a search feature using this approach and it is currently in my application and working well. Please advise. I think it may be a mal formed json string, because it should be an array.

Faiyet
  • 5,341
  • 14
  • 51
  • 66

1 Answers1

1

The Problem seams to be that jackson tryes to access the invoiceFeeId field of class PaymentsPaid, but it is a field of class PaidDetailAmounts.

I think there is some surrounding brackets missing in your Json string:

{  // <-- missing?
  "amount": [{"invoiceFeeId":...}]
}  // <-- missing?

But I am not an JSON expert, so I would try to write an simple test case that create a JSON string from some Java Objects and then parse this strings to back to Java Objects. So that the test can assert that both (sets of) objects are equals.

Then you can use the JSON String created by the test and compare it with your input, I would expect that the (missing) brackets are the difference between them.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 1
    I also used the advice given at http://stackoverflow.com/questions/8369260/jackson-throws-jsonmappingexception-on-deserialize-demands-single-string-constr and http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ just incase someone else needs it. – Faiyet Nov 09 '12 at 14:28