0

I am not much familiar with java . But I know in php we can do with json_decode function . Is there any particular function in java to decode the below string to an array?

[ {
  "type" : "panaroma",
  "image" : "http://test.com/images/aneesh/Desktop/19_september/pano-1.png",
  "location" : "{{487,393},{197,235}}",
  "trigger" : "{{487,393},{197,235}}",
  "orientation" : "portrait",      
} ]
10
  • 1
    Not with standard java, you have to use a 3rd party lib such as GSon Commented Jan 6, 2014 at 12:24
  • you will need to convert given string to JSONObject instead of JSONArray because current String contain JsonObject as root element instead of JsonArray : JSONObject jsonObject = new JSONObject(readlocationFeed); Commented Jan 6, 2014 at 12:25
  • stackoverflow.com/questions/1688099/converting-json-to-java/… Commented Jan 6, 2014 at 12:28
  • thanks guys But I have already tried the above solutions :( Commented Jan 6, 2014 at 12:58
  • The given string probably will not be parsed because it's syntactically not a valid JSONObject. Commented Jan 6, 2014 at 13:04

1 Answer 1

0

Look this: http://www.tutorialspoint.com/json/json_java_example.htm
Some like thus:

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

class JsonDecodeDemo 
{
   public static void main(String[] args) 
   {
      JSONParser parser=new JSONParser();
      String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
      try{
         Object obj = parser.parse(s);
         JSONArray array = (JSONArray)obj;
         System.out.println("The 2nd element of array");
         System.out.println(array.get(1));
         System.out.println();

         JSONObject obj2 = (JSONObject)array.get(1);
         System.out.println("Field \"1\"");
         System.out.println(obj2.get("1"));    

         s = "{}";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,]";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,,2]";
         obj = parser.parse(s);
         System.out.println(obj);
      }catch(ParseException pe){
         System.out.println("position: " + pe.getPosition());
         System.out.println(pe);
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.