Jacin Montava
Jacin Montava

Reputation: 109

Error parsing JSON with GSON

I have one problem parsing a JSON with GSON library.

I have tried to modify the JSON but I can not find where the error may be. The JSON is validated and I do not think that is the problem but the error keeps saying that it is badly formed.

Any idea how solve this error or how to parse the JSON?

I have this structure in JSON and I try to pair it with GSON but it gives me the following error.

JSON

[
  {
    "contacts":[
      {
        "contactActDate":"",
        "contactEmail":"",
        "contactIdClient":"D12345678",
        "stat":"1",
        "contactName":"Juan Perez",
        "contactPhone":"654321098",
        "contactUrlPhoto":"http://url.es/images/4271.jpg",
        "department":"",
        "contactPhoneType":3,
        "firebaseIdUser":4271,
        "group":0,
        "contactIdUser":798,
        "fecMod": 1234567
      }
    ],
    "groupName":"dev",
    "groupId":1516106171
  },
  {
    "contacts":[
      {
        "contactActDate":"",
        "contactEmail":"",
        "contactIdClient":"D12345678",
        "stat":"1",
        "contactName":"Alvaro Lopez",
        "contactPhone":"654321098",
        "contactUrlPhoto":"http://url.es/images/4271.jpg",
        "department":"",
        "contactPhoneType":3,
        "firebaseIdUser":4271,
        "group":0,
        "contactIdUser":798,
        "fecMod": 1234568
      }
    ],
    "groupName":"ant",
    "groupId":1516106173
  }
]

Process: es.wlynx.allocy, PID: 24850 java.lang.RuntimeException: Unable to resume activity {.....LaunchActivity}: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $ at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2981) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3010) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2324) at android.app.ActivityThread.access$1100(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5342) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) at dalvik.system.NativeStart.main(Native Method) Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

And these are the two models that I'm using and the call code to the GSON:

GSON Method

List<GroupsModel> groups;
String groupsContactsPath = context.getFilesDir().getAbsolutePath() + "/" + Constants.GROUPS_CONTACTS_USER_JSON_FILE_NAME;

Gson gson = new Gson();
groups = gson.fromJson(groupsContactsPath, new TypeToken<ArrayList<GroupsModel>>() {}.getType());

GroupsModel

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class GroupsModel {

    private int groupId;
    private String groupName;
    private List<ContactModel> contacts;

    public GroupsModel() {}

    public GroupsModel(int groupId, String groupName) {
        this.groupId = groupId;
        this.groupName = groupName;
    }

    public GroupsModel(JSONObject jsonObject) throws JSONException {
        this.groupId = jsonObject.getInt("groupId");
        this.groupName = jsonObject.getString("groupName");
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public List<ContactModel> getContacts() {
        return contacts;
    }

    public void setContacts(List<ContactModel> contacts) {
        this.contacts = contacts;
    }
}

and ContactModel

import android.provider.ContactsContract;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class ContactModel {
    private int firebaseIdUser;
    private int contactIdUser;
    private String contactIdClient;
    private String contactName;
    private String contactPhone;
    private int contactPhoneType;
    private String contactEmail;
    private String contactUrlPhoto;
    private String contactActDate;
    private String stat;
    private int group;
    private String department;
    private Long fecMod;

    public ContactModel() {
    }

    // fichero de favoritos
    public ContactModel(JSONObject jsonObject) throws JSONException {
        this.firebaseIdUser = jsonObject.getInt("firebaseIdUser");
        this.contactIdUser = jsonObject.getInt("contactIdUser");
        this.contactIdClient = jsonObject.getString("contactIdClient");
        this.contactName = jsonObject.getString("contactName");
        this.contactPhone = jsonObject.getString("contactPhone");
        this.contactPhoneType = jsonObject.getInt("contactPhoneType");
        this.contactEmail = jsonObject.getString("contactEmail");
        this.contactUrlPhoto = jsonObject.getString("contactUrlPhoto");
        this.contactActDate = jsonObject.getString("contactActDate");
        this.stat = jsonObject.getString("stat");
        this.department = jsonObject.getString("department");
        this.fecMod = jsonObject.getLong("fecMod");
    }

    // fichero del servidor
    public ContactModel(boolean server, JSONObject jsonObject) throws JSONException {
        this.firebaseIdUser = jsonObject.getInt("firebaseIdUser");
        this.contactIdClient = jsonObject.getString("contactIdClient");
        this.contactName = jsonObject.getString("contactName");
        this.contactPhone = jsonObject.getString("contactPhone");
        this.contactPhoneType = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
        this.contactEmail = jsonObject.getString("contactEmail");
        this.contactUrlPhoto = jsonObject.getString("contactUrlPhoto");
        this.contactActDate = jsonObject.getString("contactActDate");
        this.department = jsonObject.getString("department");
    }

    public ContactModel(int firebaseIdUser, int contactIdUser, String contactIdClient, String contactName,
                        String contactPhone, int contactPhoneType, String contactEmail,
                        String contactUrlPhoto, String contactActDate, String stat, String department, Long fecMod) {
        this.firebaseIdUser = firebaseIdUser;
        this.contactIdUser = contactIdUser;
        this.contactIdClient = contactIdClient;
        this.contactName = contactName;
        this.contactPhone = contactPhone;
        this.contactPhoneType = contactPhoneType;
        this.contactEmail = contactEmail;
        this.contactUrlPhoto = contactUrlPhoto;
        this.contactActDate = contactActDate;
        this.stat = stat;
        this.department = department;
        this.fecMod = fecMod;
    }

   /*public ContactModel(String Tipo) {
        this.firebaseIdUser = 0;
        this.contactIdUser = 0;

        this.contactIdClient = Tipo;
        this.contactName = Constants.CONTACT_DELETE_NAME;
        this.department = "";
        this.stat = Constants.STATE_INDETERMINATE;
        this.contactPhone = "";
        this.contactPhoneType = 0;
        this.contactEmail = "";
        this.contactUrlPhoto = "";
        this.contactActDate = "";
        this.fecMod = new Long(0);
    }*/

    public int getFirebaseIdUser() {
        return firebaseIdUser;
    }

    public void setFirebaseIdUser(int firebaseIdUser) {
        this.firebaseIdUser = firebaseIdUser;
    }

    public int getContactIdUser() {
        return contactIdUser;
    }

    public void setContactIdUser(int contactIdUser) {
        this.contactIdUser = contactIdUser;
    }

    public String getContactIdClient() {
        return contactIdClient;
    }

    public void setContactIdClient(String contactIdClient) {
        this.contactIdClient = contactIdClient;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public int getContactPhoneType() {
        return contactPhoneType;
    }

    public void setContactPhoneType(int contactPhoneType) {
        this.contactPhoneType = contactPhoneType;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public String getContactUrlPhoto() {
        return contactUrlPhoto;
    }

    public void setContactUrlPhoto(String contactUrlPhoto) {
        this.contactUrlPhoto = contactUrlPhoto;
    }

    public String getContactActDate() {
        return contactActDate;
    }

    public void setContactActDate(String contactActDate) {
        this.contactActDate = contactActDate;
    }

    public String getStat() {
        return stat;
    }

    public void setStat(String stat) {
        this.stat = stat;
    }

    public Long getFecMod() {
        return this.fecMod;
    }

    public void setFecMod(Long fecMod) {
        this.fecMod = fecMod;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public void showContactInfo() {
        Log.d(TAGFAF, "/**** Contact ****/");
        Log.d(TAGFAF, "Id: " + getFirebaseIdUser());
        Log.d(TAGFAF, "Phone: " + getContactPhone());
        Log.d(TAGFAF, "Stat: " + getStat());
        Log.d(TAGFAF, "/**** Contact ****/");
    }
}

Upvotes: 1

Views: 2275

Answers (2)

creonilso rodrigues
creonilso rodrigues

Reputation: 479

You can try change the value of

private int groupId;

to this:

private long groupId;

Upvotes: 0

vikas kumar
vikas kumar

Reputation: 11018

This is where you are going wrong. you are passing the fromJson a file path groupsContactsPath not the content of the file as string.

List<GroupsModel> groups;
String groupsContactsPath = context.getFilesDir().getAbsolutePath() + "/" + Constants.GROUPS_CONTACTS_USER_JSON_FILE_NAME;

Gson gson = new Gson();
groups = gson.fromJson(groupsContactsPath, new TypeToken<ArrayList<GroupsModel>>() {}.getType());

you can try this one to get the content of the file.

    private String readFile(String pathname) throws IOException {

    File file = new File(pathname);
    StringBuilder fileContents = new StringBuilder((int)file.length());
    Scanner scanner = new Scanner(file);
    String lineSeparator = System.getProperty("line.separator");

    try {
        while(scanner.hasNextLine()) {
            fileContents.append(scanner.nextLine() + lineSeparator);
        }
        return fileContents.toString();
    } finally {
        scanner.close();
    }
}

source :

Upvotes: 2

Related Questions