ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
736 stars 185 forks source link

Can't iterate over type: class com.eclipsesource.json.JsonArray #58

Closed Highlander4h closed 9 years ago

Highlander4h commented 9 years ago

Hi,

In the readme I see that it should be possible to iteratie over the elements of a JsonArray, but when I try that I get an error "Can't iterate over type: class com.eclipsesource.json.JsonArray".

I'm using JMeter 2.13 BeanShell Postprocessor and used the example from http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/

The scripts looks like this:

import com.eclipsesource.json.*;

String jsonString = prev.getResponseDataAsString();
JsonObject family = JsonObject.readFrom(jsonString);
JsonArray members = family.get("members").asArray();

for (JsonValue member : members) {
  // ...
}

The for-loops gives an error. What am I doing wrong?

ralfstx commented 9 years ago

Could you provide a stacktrace please? Which version of minimal-json are you using and which JRE?

Highlander4h commented 9 years ago

Hi Ralf, I'm using minimal-json-0.9.4 and jre build 1.8.0_51-b16 I've tried to get a stacktrace, but cannot seem to force it to give me a stacktrace. I've post a question on the JMeter board for that.

As soon as I get a stacktrace working for this I will post it.

This is the information I currently get: 2015/09/07 12:07:50 INFO - jmeter.engine.StandardJMeterEngine: All thread groups have been started 2015/09/07 12:07:50 INFO - jmeter.threads.JMeterThread: Thread started: Thread Group 1-1 2015/09/07 12:07:50 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStri . . . '' : Can't iterate over type: class com.eclipsesource.json.JsonArray 2015/09/07 12:07:50 WARN - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStri . . . '' : Can't iterate over type: class com.eclipsesource.json.JsonArray 2015/09/07 12:07:50 INFO - jmeter.threads.JMeterThread: Thread is done: Thread Group 1-1 2015/09/07 12:07:50 INFO - jmeter.threads.JMeterThread: Thread finished: Thread Group 1-1

Highlander4h commented 9 years ago

Hi Ralf, I got a response from one of the JMeter developers. He responded that AFAIK BeanShell does not support the new-style for loops. So the probleem seems to be JMeter related instead of your code.

Sorry for the inconvenience :-)

ralfstx commented 9 years ago

@Highlander4h, thanks for the update! So you should be able to fix the problem by transforming the for loop into an old-style loop (using iterator.hasNext() etc.).