-1

/* This is my json file */

{ "firstname": [ "rohit", "sagar", "sandeep", "rahul", "sweta", "priya" ] }

/* MainActivity.java */

package com.example.gsonflow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.google.gson.Gson;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Gson gson = new Gson();
    InputStream data = this.getResources().openRawResource(R.raw.safas);
    String sata = data.toString();

    Student employee = gson.fromJson(sata, Student.class);
}

}

/* Student.java */

package com.example.gsonflow;

public class Student {

private String[] firstname;


public Student(String[] firstname){
    this.firstname = firstname;
}

}

0

1 Answer 1

-1
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
JsonArray jsonArray = jsonObject.getAsJsonArray("firstname");

String[] firstNameList= new Gson().fromJson(jsonArray, String[].class);

List<String> allNames= new ArrayList<>();
allNames= Arrays.asList(firstNameList);

for (String name: allNames) {
   Log.d(TAG,name);
}   
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.