0

I am currently trying to write a multidimensional object or array in JSP and then pass it back to an AJAX call from JavaScript. Now, decoding a JSON object using AJAX I have figured out (jQuery JSON). So, I'm good on that front.

But I am lost on creating a multidimensional JSON object or array in JSP.

I have been looking at json-simple for the JSON plugin for JSP (http://code.google.com/p/json-simple/). And it's not just this plugin, but I have been unable to find any examples of multidimensional JSON objects or array examples in JSP.

Like, I get this example but it's one-dimensional:

//import org.json.simple.JSONObject;

JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
System.out.print(obj);

I would like the JSON object to have a result like this:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

Then, I'd pass it back to JavaScript and decode it.

To Sum up: How can I create a multidimensional object or array in JSP? The JSON plugin is inconsequential; whatever works, I'll be more than happy.

I would appreciate any help. Thank you in advance.

1 Answer 1

1

To create a structure like this using Gson:

{
  "results": [ {
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    } ],
  } ]
}

You could do something like this:

<%
String[] types = {"street_number"};
Hashtable address_components = new Hashtable();
address_components.put("long_name", 1600);
address_components.put("short_name", 1600);
address_components.put("types", types);
Hashtable results = new Hashtable();
results.put("address_components", address_components);

// Make sure you have the Gson JAR in your classpath
// (place it in the tomcat/classes directory)
com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(obj);  
out.print(json);
%>
Sign up to request clarification or add additional context in comments.

4 Comments

Wow. Thank you. This looks really good. However, I copied he line "JSONObject obj = new JSONObject();" (without quotes) into the JSP page and it's producing an HTTP 500 error, which means there's an error. How do I import and what library do I have to import to support JSONObject in the JSP page? Thanks again.
You would need a JSON library (a JAR-file typically) and use that. I've updated the code with an example using Gson.
Actually, I did have one more question that is related to this discussion: If you look at the example I used above to pose the question, you'll see the hashtable "address_components". How can I have multiple address_components? I'm passing data from SQL to ultimately a JSON object, but it's just taking the last set of data in the loop, does that make sense? I want the address_components hashtable to act like an array. So, in another language, I may access a value like this: results->address_components[2]->long_name. I hope that makes sense. Thank you.
Change results into an array instead of hashtable and you should be good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.