Reputation: 7268
I'm retrieving some JSON
data from my server. Sometimes this can come in the form of a JSONObject
(one objects) or a JSONArray
(multiple objects). I'm aware of how to detect if it is an array or an object... but the problem is that detecting it means that I have to have two completely separate bits of logic, one for objects and one for arrays. It's a really unclean solution that I'm surprised is not asked about more often.
What I'd like to do is: if the JSON
is a JSONObject
(one object), then convert it to a JSONArray
(with only one entry). Then I can proceed with my normal logic for interpreting JSONArray
s.
My JSON
would come in like this:
{
"Changes":
{
"Row":
{
"@ChangeId":"17192386","@Type":"U","@TableName":"Change","@PK":"g1fbb6c7-abcf-e741-846c-b499baf5845d","@ColList":"date"
},
"Data":
{
"@date":"22/05/2014 09:03:00"
}
}
}
Normally I have multiple records in the Row
and Data
objects. But in this instance, I have only one in each. My usual method is:
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject changes = jsonObj.getJSONObject("Changes");
JSONArray arrChanges = changes.getJSONArray("Row");
JSONArray arrData = changes.getJSONArray("Data");
// Cycle through arrays here
This code will crash on the .getJSONArray("Row");
line.
To avoid this, after I have the changes
object, I'd like to force the Row
and Data
objects into a JSONArray
, even if there is only one value - like above, so I can continue with my normal logic. Is there a way to do this?
Upvotes: 4
Views: 3344
Reputation: 4537
Thanks to AMC's answer I constructed the following methods to create a JSON object, store it in a file on the android device. Then if only a single object was stored then the read file method uses the convertJsonObjectToJsonArray to convert it back to an array when reading it.
public void createFile(View v) throws IOException, JSONException {
JSONArray tours = new JSONArray();
JSONObject tour;
tour = new JSONObject();
tour.put("name", "Sea and sand");
tour.put("price", 999);
tours.put(tour);
String text = tour.toString();
FileOutputStream fos = openFileOutput("tours", MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
UIHelper.displayText(this, R.id.textView1, "File written to disk:\n"+text);
}
public void readFile(View v) throws IOException, JSONException {
// read file to Buffered input stream
FileInputStream fis = openFileInput("tours");
BufferedInputStream bis = new BufferedInputStream(fis);
StringBuffer b = new StringBuffer();
while (bis.available() != 0) {
char c = (char) bis.read();
b.append(c);
}
bis.close();
fis.close();
// deserialize JSON object to text
JSONObject tours = new JSONObject(b.toString());
// If a single object is returned convert it to an Array
JSONArray toursArray = convertJsonObjectToJsonArray(tours);
StringBuilder text = new StringBuilder();
for (int i = 0; i < toursArray.length(); i++) {
String tour = toursArray.getJSONObject(i).getString("name");
text.append(tour).append("\n");
}
UIHelper.displayText(this, R.id.textView1, text.toString());
}
public JSONArray convertJsonObjectToJsonArray(Object InsideArray) {
JSONArray jsonArray;
if (InsideArray instanceof JSONArray) {
jsonArray = (JSONArray) InsideArray;
} else {
jsonArray = new JSONArray();
jsonArray.put((JSONObject) InsideArray);
}
return jsonArray;
}
Upvotes: 0
Reputation: 229
Use this method to convert JSONObject
into JSONArray
:
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject changes = jsonObj.getJSONObject("Changes");
JSONArray arrChanges = covertJsonObjectToJsonArray(changes.get("Row"));
JSONArray arrData = covertJsonObjectToJsonArray(changes.get("Data"));
public JSONArray covertJsonObjectToJsonArray(Object InsideArray) {
JSONArray jsonArray;
if (InsideArray instanceof JSONArray) {
jsonArray = (JSONArray) InsideArray;
} else {
jsonArray = new JSONArray();
jsonArray.put((JSONObject) InsideArray);
}
return jsonArray;
}
If JSONArray
is there then it will return JSONArray
, and if JSONOject
is there still it will return JSONArray
.
Upvotes: 8
Reputation: 130
if I understood correctly what your problem is:
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject changes = jsonObj.getJSONObject("Changes");
JSONArray arrChanges = null;
JSONArray arrData = null;
if (isASingleJson) {
arrChanges = new JSONArray();
arrData = new JSONArray();
arrChanges.put(changes.getJSONObject("Row"));
arrData.put(changes.getJSONObject("Data"));
} else {
arrChanges = changes.getJSONArray("Row");
arrData = changes.getJSONArray("Data");
}
//logic to handle arrays here...
Upvotes: 2
Reputation: 1362
Create on Json Array, when data from server is array then go with that same flow but if you get data from server in Object format then just create object and add that object again into same Array so You need to write logic only to parse Array in any of the case.
I hope you get my point. Happy Coddddiiiing :)
Upvotes: 0
Reputation: 3776
When you are creating your JSON you shouldcreate an array of "Row" items always, even if you have only one. This way you will not have to change any logic.
Hope it helps
Upvotes: 0