catekr / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

bug in get(), ContainerFactory #92

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

I have problems parsing the string:
{"multicast_id":5195543660248293070,"success":1,"failure":2,"canonical_ids":0,"r
esults":[{"message_id":"0:1382740990960684%921c249af9fd7ecd"},{"error":"InvalidR
egistration"},{"error":"InvalidRegistration"}]}

I'd like to have key=>value like:
success=>1
failure=>2
results=>[{"message_id":"0:1382740990960684%921c249af9fd7ecd"},{"error":"Invalid
Registration"},{"error":"InvalidRegistration"}]

But get("results") returns:
[{message_id=0:1382739363231902%921c249af9fd7ecd}, {error=InvalidRegistration}, 
{error=InvalidRegistration}]
As you see, the "" got lost and : became =

So I cannot continue parsing results to
0=>{message_id=0:1382739363231902%921c249af9fd7ecd}
1=>{error=InvalidRegistration}
2=>{error=InvalidRegistration}

Please test this function and try to find a solution:

    public static void func()
    {

        String str="{\"multicast_id\":5375760673945275273,\"success\":1,\"failure\":2,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1382739363231902%921c249af9fd7ecd\"},{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"}]}";
        System.out.println("original: "+str);

        JSONParser parser = new JSONParser();
        ContainerFactory containerFactory = new ContainerFactory(){
            public List creatArrayContainer() {
                return new LinkedList();
            }

            public Map createObjectContainer() {
                return new LinkedHashMap();
            }
        };

        try{
            Map json = (Map)parser.parse(str, containerFactory);
            System.out.println("wtf: " + json.get("results"));

            /*
            String res = json.get("results").toString();
            JSONParser parser2 = new JSONParser();
            Object obj=parser2.parse(res);
            JSONArray array=(JSONArray)obj;
              for(int a = 0; a < array.size(); a++)
                  System.out.println("results["+a+"] = "+array.get(a));
              */

        }

        catch(ParseException pe){
            System.out.println(pe);
        }
    }

Thanks.

Original issue reported on code.google.com by Dominik....@googlemail.com on 25 Oct 2013 at 11:05

GoogleCodeExporter commented 8 years ago

Original comment by fangyid...@gmail.com on 15 Jul 2014 at 3:15