Wednesday, January 2, 2013

[android-developers] "The type of the expression must be an array type but it resolved to JSONArray" compile error

I get this message:

"The type of the expression must be an array type but it resolved to
JSONArray"

It happens on this line in the below java code:

JSONObject unit = units[i];

units[i] should be holding { "id":3001, "markers": [...]}.


I have json data that looks like this:


result = {
"status":200,
"error":null,
"data":
{
"units":
[
{
"id":3001,
"markers":
[

{"latitude":"28.92977431","longitude":"-82.01933333","address":"unknown"},

{"latitude":"22.92977431","longitude":"-42.01933333","address":"unknown"},

{"latitude":"33.92977431","longitude":"-66.01933333","address":"unknown"}
]
}
]
}
}


Here's my java code:


Map<String,String> reportData1 = new HashMap<String, String>();
List<Map<String , String>> reportData = new
ArrayList<Map<String,String>>();

String result = QWebservice.getWebResource(
WEBSERVICE,
query);

try {
JSONObject jsonResult = new JSONObject(result);
JSONObject data = jsonResult.getJSONObject("data");
JSONArray units = data.getJSONArray("units");

for(int i = 0 ; i < units.length(); i++){
JSONObject unit = units[i];
JSONObject reports = unit.getJSONArray("markers");

for(int j = 0; j < reports.length(); j++){
report = reports[j];
Iterator iter = report.keys();

while(iter.hasNext()){
String key = (String)iter.next();
if(key.equals("latitude")){
reportData1.put("latitude", report.getString(key));
reportData.add(j,reportData1);
}
if(key.equals("longitude")){
reportData1.put("longitude", report.getString(key));
reportData.add(j,reportData1);
}
if(key.equals("address")){
reportData1.put("address", report.getString(key));
reportData.add(j,reportData1);
}
}
}
}

} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate