I want to convert json data to string
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
public static void main(String[] args) throws Exception
{
URL url = new URL("http://192.168.1.13/test/ProductWb.php?productId=9");
HttpURLConnection conn ;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(60);
conn.setRequestProperty("Accept", "application/json");
String json="";
json = readUrl(conn);
System.out.println(json);
JSONObject jsonObject=new JSONObject(json);
JSONArray jarray=jsonObject.getJSONArray("modeles");
JSONObject modele= jarray.getJSONObject("modele");
for (int i=0;i<modele.length();i++) {
System.out.println(modele(i).getString("id_product"));
System.out.println(modele(i).getString("meta_title"));
System.out.println("*********");
}
}
its show me the json data but give me this the error:
{"modeles":[{"modele":{"id_product":"9","id_shop":"1","id_lang":"4","description":null,"description_short":"<pre>Peugeot 208<\/pre>","info_prix":"","info_1":null,"info_2":null,"info_3":null,"info_4":null,"info_5":null,"link_rewrite":"208","meta_description":"Peugeot 208","meta_keywords":"peugeot 208","meta_title":"Peugeot 208","name":"208","available_now":"","available_later":""}}]}
Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array.
at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
at com.autoreduc.services.TestProduct.main(TestProduct.java:59)
help me if you have any solution. thanks in advance