0

This is my base object:

class Obj
{
  float t;
  String data;
  int y;
}

json example: {"t": 11.0,  "data": "7432",  "y": 0 }

I need to parse in java, using Gson, an input like this:

{
 "key1":[{"t": 4.0,  "data": "saer",  "y": 0 },..,{"t": 6.6,  "data": "dfs",  "y": 4 }],
 "key2":[{"t": 33,50,  "data": "3r2",  "y": 1 },...,{"t": 43.0,  "data": "54t",  "y": 3 }],
 ...
}

In PHP i'm using this script:

    $decoded = json_decode($_POST['json']);             
    foreach ($decoded as $key => $single)
    {
        foreach ($single as $value) 
        {   
            // using $key, $value->t,...
        }           
    }

How can I do the same thing in java? I'm confused how retrive the "key" values.

Thanks

1 Answer 1

2

In simple way you can parse it as

final Type typeOf= new TypeToken<Map<String,List<Obj>>>(){}.getType();
final Map<String,List<Obj>> map = new Gson().fromJson(Your_Json_String, typeOf);
// get value 
final  List<Obj> list = map.get("key1");
Sign up to request clarification or add additional context in comments.

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.