1

I have previously used GSON, which automatically transfers the data as long as my custom object has a variable with the same name. However, this time, I'm also intrested in the name, or ID, of the object. The object only contains a single long. Example of how it looks:

{"1":123,
 "2":124,
 "4":125,
 "5":126,
 "6":127}

As you can see, the list don't necessarily contain all sequent IDs so I cannot just create a list. How would you solve the problem?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user3600338
  • 119
  • 1
  • 10

2 Answers2

2

Instead of deserializing to a specific custom object, just deserialize to Map<String, Integer>:

Type type = new TypeToken<Map<String, Integer>>(){}.getType();
Map<String, Integer> result = gson.fromJson(jsonString, type);
nickb
  • 59,313
  • 13
  • 108
  • 143
0

Use jython:

import json
json_data = json.loads(your_string_above)
ids = json_data.keys()
# ids now contains [u'1', u'2', u'4', u'5', u'6']

Hope that helps.

hd1
  • 33,938
  • 5
  • 80
  • 91