0

I am parsing a JSON file with weekdays and times inside those weekdays. Problem is that whenever I parse them, it's a huge mix up. For example this is my json file:

{
"root1": {
"test": {
  "monday": {
    "08:00-12:20": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "tuesday": {
    "08:00-11:50": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "wednesday": {
    "08:00-11:50": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "friday": {
    "08:00-11:50": {
      "value1": "value2",
      "value3": "value4"
    }
  }
},
"test2": {
  "saturday": {
    "08:05-11:00": {
      "value1": "value2",
      "value3": "value4"
    }
  }
},
"test3": {
  "monday": {
    "12:45-15:10": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "tuesday": {
    "08:55-11:50": {
      "value1": "value2",
      "value3": "value4"
    },
    "15:30-17:55": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "wednesday": {
    "16:35-18:00": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "thursday": {
    "08:00-09:40": {
      "value1": "value2",
      "value3": "value4"
    },
    "10:00-12:25": {
      "value1": "value2",
      "value3": "value4"
    }
  },
  "friday": {
    "16:35-18:00": {
      "value1": "value2",
      "value3": "value4"
    }
  }
}
}
}

So you see the weekdays are in the correct order. But if I parsed them they're like this for example:

  • thursday
  • friday
  • monday
  • wednesday
  • tuesday

How can I parse them into the right order?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
user754730
  • 1,341
  • 5
  • 31
  • 62
  • Following the JSON specification, [objects are unordered](http://stackoverflow.com/a/4920304/1073063). – Pablo Jun 07 '13 at 08:41
  • Ok thanks. But how can I parse them into the right order? Or sort them? – user754730 Jun 07 '13 at 08:43
  • You probably can't, unless there is some implementation that doesn't follow the specification. You'll have to order them yourself after parsing the file. If you want ordered results, you should use arrays. – Pablo Jun 07 '13 at 08:47

2 Answers2

1

You can use JsonReader for stream reading - and control order.

matreshkin
  • 2,199
  • 18
  • 28
0

you have to use a JSONArray instead of a JSONObject. In fact the JSONArray preserves the order

Blackbelt
  • 156,034
  • 29
  • 297
  • 305