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",      
} ]
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Anish
  • 4,262
  • 6
  • 36
  • 58
  • 1
    Not with standard java, you have to use a 3rd party lib such as [GSon](https://code.google.com/p/google-gson/) – A4L Jan 06 '14 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); – Janty Jan 06 '14 at 12:25
  • http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182 – Gangnus Jan 06 '14 at 12:28
  • thanks guys But I have already tried the above solutions :( – Anish Jan 06 '14 at 12:58
  • The given string probably will not be parsed because it's syntactically not a valid JSONObject. – JimmyB Jan 06 '14 at 13:04
  • What you have is an array of one element containing an "object". Any standard JSON kit should be able to decode that into a List of one element containing a Map. (@janty is quite wrong.) – Hot Licks Jan 06 '14 at 13:11
  • @HannoBinder - You are right -- it's not a valid JSON "object". It is, however, a perfectly valid JSON array containing an object. See json.org. – Hot Licks Jan 06 '14 at 13:12
  • What specific problem are you having with this? If you convert to a JSONArray (assuming that's the kit you're using) then you can easily extract array element zero which will be a JSONObject containing the "guts" of the structure. (You have not met the requirements of a valid question, since you have not described a problem.) – Hot Licks Jan 06 '14 at 13:15
  • @Hot Licks, is a bare JSON array without an enclosing object a valid JSON structure anyway? – JimmyB Jan 06 '14 at 18:50
  • Have you read the JSON spec at json.org??? – Hot Licks Jan 06 '14 at 19:41

1 Answers1

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);
      }
   }
}
Rafael Soufraz
  • 974
  • 3
  • 11
  • 28