Domanda

I have a json pattern like this

objects: [
    -{
        category: {
            id: 4,
            image: "category image url",
            title: "LIGHT"
        },
        create_time: "2014-03-20T17:57:23",
        file_url: "file url",
        id: 26,
        title: "Note",
        user: {
            avatar: "thumb url",
            id: 12,
            name: "user name"
        }
    }
    -{
        category: {
            id: 1,
            image: "category image url",
            title: "HEAVY"
        },
        create_time: "2014-03-20T17:57:23",
        file_url: "file url",
        id: 25,
        title: "Photo",
        user: {
            avatar: "thumb url",
            id: 2,
            name: "user name"
        }
    }
    .
    .
    .
]

When i parsed this block everything is okay until when i want to put parsed nodes to Hashmap. for parsing i defin nodes and object as TAG like

public static final String TAG_CAT_ID           = "id";
public static final String TAG_TONE_ID          = "id";
public static final String TAG_USER_ID          = "id";
public static final String TAG_CAT_TITLE        = "title";
public static final String TAG_TITLE            = "title";

then i parsed them in For loop and save results as String like this:

String cat_id                   = category.getString(TAG_CAT_ID);
String cat_title                = category.getString(TAG_CAT_TITLE);
String file_id                  = file.getString(TAG_TONE_ID);
String file_title               = file.getString(TAG_TITLE);
String user_id                  = user.getString(TAG_USER_ID);

As i should say....category, file and user defined as JSON Object. till now everythin is okay and i can get result as i want:

cat_id     -> 4
cat_title  -> LIGHT
file_id    -> 26
file_title -> Note
user_id    -> 12
----------------------
cat_id     -> 1
cat_title  -> HEAVY
file_id    -> 25
file_title -> Photo
user_id    -> 2

after this section i put this result in HashMap like this:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CAT_ID, cat_id);
map.put(TAG_TONE_ID, tone_id);
map.put(TAG_USER_ID, user_id);

disaster happened!! for each object...hash map returns only one id!!!!! same id for cat_id, file_id, user_id.......and one title for cat_title and file_title!!!

where is my wrong?!

È stato utile?

Soluzione

public static final String TAG_CAT_ID           = "id";
public static final String TAG_TONE_ID          = "id";
public static final String TAG_USER_ID          = "id";

These ID-s are same, so when you put the values, you use the same key. This way you overwrite the "id" key's value 3 times.

Your code equivalent to this:

map.put("id", cat_id);
map.put("id", tone_id);
map.put("id", user_id);

You should define your ID-s unique.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top