问题描述:

I want to Load data as String variable from JSON in Java android studio.

My JSON is :

{

"server_response": [

{

"name": "1",

"email": "[email protected]",

"mobile": "990-100010000"

},

{

"name": "2",

"email": "Tanim Khan ",

"mobile": "[email protected] "

},

{

"name": "3",

"email": "Rezaul Hasan ",

"mobile": "[email protected] "

}

]

}

And My target String list as Like :

 String email[] = {

" Mirpur-13, Kafrul , Govt.Unani Medical College & Hospital, Dhaka",

" Sher-E-Bangla Nagar , Shaheed Sohrawardi Medicel College Hospital, Dhaka",

" Shahbag Square , Birdem General Hospital, Dhaka",

" Shahbag circle , BSMMU, Dhaka",

" Shaymoli bus stand , Dhaka Child Hospital, Dhaka",

" Bokshi Bazar, 100 Ramna , Dhaka Medical College Hospital, Dhaka",

" Nayabazar , Dhaka City General Hospital, Dhaka",

" Sher-E-Bangla Nagar , National Heart Institute & Hospital, Dhaka",

" Sher-E-Bangla Nagar, National Mental Health Institute & Hospital, Dhaka",

" Shayamoli, Sher-E-Bangla Nagar , National Ophthalmological Institute & Hospital, Dhaka",

" Mahakhali , National Cancer Research Institute & Hospital, Dhaka",

" Sher-E-Bangla Nagar , National Kidney Institute & Hospital, Dhaka",

" Sher-E-Bangla Nagar , National Orthopedics Hospital, Dhaka",

" Mirpur , Kidney Foundation Bangladesh, Dhaka",

" Mahakhali Kacha Bazar , ICDDRB, Dhaka"};

String mobile[] = {

"02-8012048",

"02-9130800",

"02-8616641",

"02-9661051",

"02-8116061",

"02-8626812",

"02-7390860",

"02-9122560",

"02-9118171",

"02-9118336",

"02-9880078",

"02-91365560",

"02-9144190",

"02-8055827",

"02-8806523",};

Now i want to set all "email" data of JSON into product[] array and "mobile" in name[] array. Please help me just how i pass data to those array.

网友答案:

You can get JSON Simple from [here]. Here is a [little tutorial].

Once you have downloaded it and added the jar to your project, the JSON-decoding mechanism would be something like this...

String st = "{\"server_response\":[{\"name\":\"1\",\"email\":\"[email protected]\",\"mobile\":\"990-100010000\"},{\"name\":\"2\",\"email\":\"Tanim Khan \",\"mobile\":\"[email protected] \"},{\"name\":\"3\",\"email\":\"Rezaul Hasan \",\"mobile\":\"[email protected] \"}]}";
JSONParser parser = new JSONParser();
try {
    Object o = parser.parse(st);
    JSONObject obj = (JSONObject) o;
    System.out.println(obj); // {"server_response":[{"name":"1","mobile":"990-100010000","email":"[email protected]"},{"name":"2","mobile":"[email protected] ","email":"Tanim Khan "},{"name":"3","mobile":"[email protected] ","email":"Rezaul Hasan "}]}

    JSONArray arr = (JSONArray) obj.get("server_response");

    List<String> email = new ArrayList<String>();
    List<String> mobile = new ArrayList<String>();

    for (Object each: arr) {
        JSONObject a = (JSONObject) each;
        email.add(a.get("email").toString().trim());
        mobile.add(a.get("mobile").toString().trim());
    }

    System.out.println(email); // [[email protected], Tanim Khan, Rezaul Hasan]
    System.out.println(mobile); // [990-100010000, [email protected], [email protected]]

} catch (org.json.simple.parser.ParseException e) {
    e.printStackTrace();
}

You might notice that the mobile list contains things other than phone numbers. That is because of the JSON data you have provided which kinda looks like this...

{
    "server_response": [
        {
            "name": "1",
            "email": "[email protected]",  <--- good
            "mobile": "990-100010000"          <--- good
        }, 
        {
            "name": "2",
            "email": "Tanim Khan ",            <--- not good
            "mobile": "[email protected] "       <--- not good
        }, 
        {
            "name": "3",
            "email": "Rezaul Hasan ",          <--- not good
            "mobile": "[email protected] "      <--- not good
        }
    ]
}

I'd suggest you fix your JSON data first. "Rezaul Hasan " is really not an email ID, and "[email protected] " is really not a cell-phone number.

Let me know if there are any questions! :)

网友答案:

You can use Gson library to deserialize json into Map (Or any java bean) and then populate the lists for emails and mobile numbers, e.g.:

Gson gson = new Gson();
Map<String, Object> data = gson.fromJson("{\"server_response\": [{    \"name\": \"1\",    \"email\": \"[email protected]\",    \"mobile\": \"990-100010000\"}, {    \"name\": \"2\",    \"email\": \"Tanim Khan \",    \"mobile\": \"[email protected] \"}, {    \"name\": \"3\",    \"email\": \"Rezaul Hasan \",    \"mobile\": \"[email protected] \"}]}", Map.class);
List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("server_response");
List<String> emails = new ArrayList<>();
List<String> numbers = new ArrayList<>();
for(Map<String, Object> element : list){
    emails.add(element.get("email").toString().trim());
    numbers.add(element.get("mobile").toString().trim());
}
System.out.println(emails);
System.out.println(numbers);
相关阅读:
Top