1

In my java spring project, I have string of json array with objects.I wan to fetch data from string and then save to the database.

So,I have: pojo class

public class Contact {
    private String id;
    private String name;
    private String mobile;
    //getters and settrs & constructors
    }

and I want to fetch data like below code:

String stringdata=["{"id":"1","mobile":"1860108","name":"Intex"}",
"{"id":"21","mobile":"777717717","name":"pari"}",
"{"id":"26","mobile":"172676236","name":"pari2"}"];

      try {
    Contact contact1 = new Contact();

         try {

     contact1 = new Gson().fromJson(contactreceive, Contact.class);

        } 
          catch (UnsupportedEncodingException uee) {

           return new ResponseEntity<Object>("failed", HttpStatus.EXPECTATION_FAILED);
        }

        Contact contact = new Contact(contact1.getId(),contact1.getName(),contact1.getMobile());
         userDao.saveContact(contact);

           return new ResponseEntity<Object>("created", HttpStatus.CREATED);



        } catch (Exception e) {

            logger.error("Mobile User Signup > Error: " + e.getMessage());

            return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
        }
    }

But I am getting error:

Mobile User Signup > Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

without remove array,How can I fetch that contacts?

5
  • 2
    try to wrap it like { "data": [your array goes here] } Commented Jan 4, 2016 at 8:09
  • you are wasting other users time by showing a code example that contains variables that are not declared and have no explanation. You declare stringdata that is not use and use userDao that is not declared. Furthermore you should format your code properly Commented Jan 4, 2016 at 8:41
  • @Christian I think you didn't realize this is only a piece of code. If he posted an error sentence, then the code compiled. Concerning time wasting, everybody who posts here at SO (including you) is wasting other user's time. :) Commented Jan 4, 2016 at 8:51
  • Only a suggestion: if you cannot solve his problem, just go to another question or go read a book. Don't post anything. Commented Jan 4, 2016 at 8:52
  • @Paulo, you are right. Commented Jan 4, 2016 at 9:19

2 Answers 2

1

Your JSON String starts and ends with [] so GSON or Jackson or any other parser will parse an array. So in order to fix this you have to do this

List<Contact> contact1 = new ArrayList<>();
contact1 = new Gson().fromJson(contactreceive, Contact.class);
Contact contactObject = contact1.get(0);
Contact contact = new Contact(contactObject.getId(),contactObject.getName(),
                              contactObject.getMobile());  
Sign up to request clarification or add additional context in comments.

2 Comments

Contact contactObject = contact1[0]; I am getting error :-- The type of the expression must be an array type but it resolved to List<Contact>
@Preeti oops, i fixed it
0

The content contains an array while you're expecting single class. You need to pass type of List of the Contact class, do so for example by declaring dummy field and getting its type:

private static List<Contact> contactTypeField;

...
List<Contact> contacts = new Gson().fromJson(contactreceive,
        MyClass.getDeclaredField("contactTypeField").getGenericType());

1 Comment

The class from your example that contains the actual parsing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.