0

I am trying to convert a string to jsonarray

I want to store string in MYsql

Code -

<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>

<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%>
<%@page import="net.sf.json.*"%>

<%
try 
{
JSONArray jArray = new JSONArray();
jArray.add("test");
String s = jArray.toString();
JSONArray newJArray = new JSONArray(s);
}
catch(Exception ex)
{
ex.printStackTrace(response.getWriter());  
} 
%>

But it gives error The constructor JSONArray(String) is undefined

I want to get jsonarray string back as a JSONArray for further use. Is this possible?

2
  • Yes-consider looking at the actual api docs and usage notes. Commented Nov 5, 2013 at 4:12
  • I do not know much, but fromObject may be a solution Commented Nov 5, 2013 at 4:43

2 Answers 2

0
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);  
JSONArray jsonArray = JSONArray.fromObject(jsonString,config); 
List list=JSONArray.toList(jsonArray,beanClass);  
Sign up to request clarification or add additional context in comments.

Comments

0

The import you are using is:

 <%@page import="net.sf.json.*"%>

instead you should use:

<%@page import="org.json.JSONArray"%>

You can download the jar from here

Then use the code :

    <%
    JSONArray jArray = new JSONArray();
    jArray.put("test");
    String s = jArray.toString();
    JSONArray new_array = new JSONArray(s);
    %>

Comments

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.