我从服务器接收到JSONArray,并使用Jsonobject创建了新的Jsonarry,我想将其发送到另一个活动,当我通过异常从jsonarray获取数据时,我也收到了另一个活动
JSOn数组
{"restaurant_name":[{"restaurantname":"Tikka Mehal","fooditemname":"Chicken Fajita Pizza","fooditemprice":"800"},{"restaurantname":"Saege","fooditemname":"Chicken Fajita Pizza","fooditemprice":"800"}]}
AsynTask类
JSONObject sending_object = new JSONObject();
JSONArray sending_array = new JSONArray();
else
{
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("restaurant_name");
for(int i =0;i<jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
sending_array.put((jsonObject1.getString("restaurantname")));
sending_array.put(jsonObject1.getString("fooditemname"));
sending_array.put(jsonObject1.getString("fooditemprice"));
}
sending_object.put("restaurant_name",sending_array);
Intent intent = new Intent(context,RestaurantFoodItemActivity.class);
intent.putExtra("restaurant_names",sending_object.toString());
context.startActivity(intent);
}
第二项活动
ArrayList餐厅;
Intent bundle = getIntent();
try {
JSONObject jsonObject = new JSONObject(bundle.getStringExtra("restaurant_names"));
JSONArray jsonArray = jsonObject.getJSONArray("restaurant_name");
for(int i =0;i<jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);// there through exception
String r_name = jsonObject1.getString("restaurantname");
String f_name = jsonObject1.getString("fooditemname");
String f_price = jsonObject1.getString("fooditemprice");
restaurants.add(new Restaurant(r_name,f_name,f_price));
}
答案 0 :(得分:0)
使用Parcelable或Serializable接口。 尝试在实现Parcelable的Some Restaurant Object中封装json数据。 示例:
public class Person implements Parcelable {
private String firstName;
private String lastName;
private List<Person> konwnPersons;
private List<String> qualifications;
public Person() {
super();
}
public Person(Parcel parcel) {
this.firstName = parcel.readString();
this.lastName = parcel.readString();
this.qualifications = parcel.readArrayList(null);
this.konwnPersons = parcel.createTypedArrayList(Person.CREATOR);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<String> getQualification() {
return qualification;
}
public void setQualifications(List<String> qualifications) {
this.qualifications = qualifications;
}
public List<Person> getKonwnPersons() {
return konwnPersons;
}
public void setKonwnPersons(List<Person> konwnPersons) {
this.konwnPersons = konwnPersons;
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.firstName);
dest.writeString(this.lastName);
dest.writeList(this.qualifications);
dest.writeTypedList(this.konwnPersons);
}
}
现在发送数据使用此:
Intent secondActivityIntent = new Intent(MainActivity.this,Main2Activity.class);
Person person = new Person();
person.setFirstName("Rajnish");
person.setLastName("Suryavanshi");
secondActivityIntent.putExtra("data",person);
startActivity(secondActivityIntent);
要获取数据,请使用:
Person person = (Person)getIntent().getParcelableExtra("data");
答案 1 :(得分:0)
也许是例外,因为您只是将字符串放在json数组中而不是json对象中,并且在下一个活动中,您尝试从不存在的数组中获取jsonObject,您可以进行以下更改
var array = [
{user:"Julia", startTime: "2019-04-09T11:22:36"},
{user:"Charles", startTime:"2019-04-10T11:22:36"},
{user:"Lisa", startTime:"2019-04-10T11:22:36"},
{user:"Hank", startTime:"2019-04-11T11:22:36"},
{user:"Hank", startTime:"2019-04-08T11:22:36"},
];
function compare(a, b) {
const startA = new Date(a.startTime).getTime();
const startB = new Date(b.startTime).getTime();
return startB - startA;
}
console.log(array.sort(compare));
如果您在第二个活动中保留相同的代码,则应该可以使用
答案 2 :(得分:0)
您为什么不使用HashMap?
例如,在发送意图时:
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent =
new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);
然后在接收活动中:
HashMap<String, String>
hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
修改
阅读您的评论后,我认为最好的方法是使用数据库并在获取信息后存储您的信息,然后在第二次活动中使用它们