0

I have json string data like:

 {"contactlist":["{"id":"1","mobile":"186010855","name":"Intex"}","{"id":"212","mobile":"980067","name":"pari"}"]}

I want to fetch all data means id,mobile and name for all.. So, I have Contact Class :

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

and for fetching I have tried:

       String stringdata=
              "{"contactlist":["{"id":"1","mobile":"186010855","name":"Intex"}","
                {"id":"212","mobile":"980067","name":"pari"}"]}";
         try{ 
            Contact contact1 = new Contact();
            contact1 = new Gson().fromJson(contactreceive, Contact.class);
            Contact contact = new Contact(contact1.getId(),contact1.getName(),
                                            contact1.getMobile());

                System.out.println (contact);
          }catch(Exception e){

           }

But I can not fetch data like this..how can I fetch this json data?

1
  • 1
    I think you are deserializing and not fetching it. And JSON also is an invalid JSON. Please post a valid one. Commented Jan 4, 2016 at 12:55

4 Answers 4

2

Your JSON is invalid, as per @ShubhamChaurasia 's comment. The following JSON validates:

{
    "contactlist": [{
        "id": "1",
        "mobile": "186010855",
        "name": "Intex"
    },
    {
        "id": "212",
        "mobile": "980067",
        "name": "pari"
    }]
}

Escape this in Java as you have already been shown (with \") and this will stop your JSON validation errors. I recommend combining this with @Joopkins ' or @JanTheGun 's answer.

A good site I use for JSON validation is http://jsonlint.com/.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to escape the double quotes to get a valid json string. Then you can read the json array like this:

String stringdata = "{\"contactlist\":[{\"id\":\"1\",\"mobile\":\"186010855\",\"name\":\"Intex\"},{\"id\":\"212\",\"mobile\":\"980067\",\"name\":\"pari\"}]}";

JsonElement jsonElement = new JsonParser().parse(stringdata);
JsonObject jsonOject = jsonElement.getAsJsonObject();
JsonArray jsonArray = jsonOject.getAsJsonArray("contactlist");

for (JsonElement element : jsonArray) {
    JsonObject obj = element.getAsJsonObject();

    String id = obj.get("id").toString();
    String name = obj.get("name").toString();
    String mobile = obj.get("mobile").toString();

    Contact contact = new Contact(id, name, mobile);
    System.out.println("id: " + contact.getId());
    System.out.println("name: " + contact.getName());
    System.out.println("mobile: " + contact.getMobile());
    System.out.println("");
}

3 Comments

Error: com.google.gson.stream.MalformedJsonException: Unterminated array at line 1 column 21
Did you escape the double quotes in your json string?
Yes, I have used replace("\\", "");
1

You can convert the stringdata into a JSONArray containing a JSONObject for each contact.

    JSONObject contactData = new JSONObject(stringdata);
    JSONArray contactArray = contactData.getJSONArray("contactlist");
    //Create an array of contacts to store the data
    Contact[] contacts = new Contact[contactArray.length()];
    //Step through the array of JSONObjects and convert them to your Java class
    Gson gson = new Gson();
    for(int i = 0; i < contactArray.length(); i++){
         contacts[i] = gson.fromJson(
              contactArray.getJSONObject(i).toString(), Contact.class);
          }

Now you have an array of Contacts from your raw JSON data.

1 Comment

Error: Expected a ',' or ']' at 20 [character 21 line 1]
0
JSONObject contactData = new JSONObject(stringdata);
JSONArray contactArray = contactData.getJSONArray("contactlist");
for(int i=0;i<contactArray .length;i++){
    JSONObject jsonObject=contactArray.getJSONObject(i));
    String id=jsonObject.getString("id")

    String key=jsonObject.getString("key")
}

Comments

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.