Java 无法在android中处理json数据 [{“acteurid”:“86744570”,“productions”:“{“variete”:“Riz”,“Reclete”:“10000”}],“nom”:“Charles Diouf”},{“acteurid”:“6535150”,“productions”:[{“variete”:“Riz”,“Reclete”:“1000”}],“nom”:“Daba Diouf”},{“acteurid”:“86817462”,“productions”:“{“variete”:“Riz”,“Reclete”:“8000”},{“Diel Ndour”;“Actnomid”:“14047190”,“variete”:“productions”,“Reclete”:“10000”},{“variete”:“Mais”,“Reclete”:“1000”},{“variete”:“Mais”,“Reclete”:“2000”},{“variete”:“Riz”,“Reclete”:“5000”},{“variete”:“Ble”,“Reclete”:“8000”},““nom”:“Hamady Diouf”}]
如何读取上述json数据?我想在Java 无法在android中处理json数据 [{“acteurid”:“86744570”,“productions”:“{“variete”:“Riz”,“Reclete”:“10000”}],“nom”:“Charles Diouf”},{“acteurid”:“6535150”,“productions”:[{“variete”:“Riz”,“Reclete”:“1000”}],“nom”:“Daba Diouf”},{“acteurid”:“86817462”,“productions”:“{“variete”:“Riz”,“Reclete”:“8000”},{“Diel Ndour”;“Actnomid”:“14047190”,“variete”:“productions”,“Reclete”:“10000”},{“variete”:“Mais”,“Reclete”:“1000”},{“variete”:“Mais”,“Reclete”:“2000”},{“variete”:“Riz”,“Reclete”:“5000”},{“variete”:“Ble”,“Reclete”:“8000”},““nom”:“Hamady Diouf”}],java,android,json,android-listview,Java,Android,Json,Android Listview,如何读取上述json数据?我想在列表视图中显示它,如下所示: 名字 品种 回忆 .变种 雷克特先生 。 . . 名字 品种 回忆 我尝试了以下代码,但列表未显示。我已 FATAL EXCEPTION: main java.lang.NullPointerException at pcom.jsontest.MyCustomAdapter.getView(MyCustomAdapter.java:32) 代码是 for(int i=0;i<jarray.length();
列表视图中显示它,如下所示:
名字
品种
回忆
.变种
雷克特先生
。
.
.
名字
品种
回忆
我尝试了以下代码,但列表未显示。我已
FATAL EXCEPTION: main
java.lang.NullPointerException at
pcom.jsontest.MyCustomAdapter.getView(MyCustomAdapter.java:32)
代码是
for(int i=0;i<jarray.length();i++){
JSONObject json=jarray.getJSONObject(i);
JSONArray prodar=json.getJSONArray("productions");
for(int j=0;j<prodar.length();j++){
JSONObject prod=prodar.getJSONObject(j);
Production production = new Production(prod.getString("variete"),prod.getString("recolte"));
productions.add(production);
}
Producteur producteur=new Producteur(json.getString("acteurid"),productions,json.getString("nom"));
producteurs.add(producteur);
}
MyCustomAdapter adapter = new MyCustomAdapter(this,producteurs);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
for(inti=0;i如果您将json可视化,例如
您将看到整个JSON的层次结构
从这一点出发,我建议您将模型设置为:
public class Model {
int mId;
List<Product> mProducts;
String mName;
}
公共类模型{
int mId;
列出产品清单;
字符串mName;
}
您应该能够根据这个模型解析json,然后使用它来可视化列表中的项目或任何您想要的地方
例如,当你
List<Model>;
列表;
你可以用
ArrayAdapter<Model>;
ArrayAdapter;
为了完成dilix的示例,假设您的模型名为Producteur
:
public class Producteur {
// Private attributes
private int mId;
// List of Produit, which is another model or object (call it however you want) created by you.
private List<Produit> mProduits;
private String mNom;
// Attributes getters and setters (they are optionnal in this example, but it's always good idea to have them)
public int get_mId() { return this.mId; }
public void set_mId(int id) { this.mId = id; }
public List<Produit> get_mProduits() { return this.mProduits; }
public void set_mProduits(List<Produit> produits) { this.mProduits = produits; }
public String get_mNom() { return this.mNom; }
public void set_mNom(String nom) { this.mNom= nom; }
// Constructor
public Producteur(int id, List<Produit> produits, String nom) {
this.mId = id;
this.mProduits = produits;
this.mNom = nom;
}
}
现在是在数据读取中使用这两个对象的时候了:
// Instead of a hashmap, we'll be using a list of "Producteur"
List<Producteur> producteurs = new List<Producteur>();
JSONArray jarray=new JSONArray(resSel);
//String acteur_id="";
for(int i=0;i<jarray.length();i++){
JSONObject json = jarray.getJSONObject(i);
int id = json.getInt("acteurid");
// Note the name is not part of the "productions" JSONArray
String nom = json.getString("nom");
// Here we'll declare the productor's productions
List<Produit> productions = new List<Produit>();
JSONArray prodar=json.getJSONArray("productions");
for(int j=0;j<prodar.length();j++){
JSONObject prod=prodar.getJSONObject(j);
String variete = prod.getString("variete");
int recolte = Integer.parseInt(prod.getString("recolte"));
// We'll create a new "Produit" at each loop and add it to the productor's product list.
Produit unProduit = new Produit(variete, recolte);
productions.add(unProduit);
}
// We'll create a new productor at each loop and add it to the productors list
Producteur unProducteur = new Producteur(id, productions, nom);
producteurs.add(unProducteur);
}
//我们将使用“Producteur”列表,而不是哈希映射
List PRODUCTURS=新列表();
JSONArray jarray=新JSONArray(RESEL);
//字符串acteur_id=“”;
对于(int i=0;我不知道你的意思。第一项是字符串,第二项是要解析的字符串。这就是你需要解析的所有数据吗?因为这是一个只有一项的ArrayList([{},{},…]
)
,您将获得全部数据。我知道这不是您想要的?您想做什么?@BishopBarber我想显示我发布的json数据中的值。我使用json.put(id,member)在application server中创建了它在servlet中。第二个值包含子值。等等,那么您不想读取json数据,而是将json数据发送到您的servlet?@BishopBarber抱歉,我已经编辑了我的帖子。我想读取json数据。列表无法显示。我的list_item.xml正确吗?致命异常:main java.lang.IllegalStateException:ArrayAdapter需要资源我想成为一名TextView@PaulNgom您的布局看起来不错。您是否实现了如图所示的自定义ArrayAdapter?如果是,您可以发布代码吗?@PaulNgom您正在进步,干得好!尽管我在您的代码中发现了两个问题。首先,在自定义ArrayAdapter中,您有以下行:TextView name=(TextView)convertView.findViewById(R.id.name);
注意,在列表项.xml
布局中,TextView的id是nom
而不是name
@PaulNgom第二个错误是填充产品列表视图。这一行:ArrayAdapter ProductionAdapter=new ArrayAdapter(getContext(),R.layout.list\u产品项,productions);
将不起作用。如果您检查正在使用的ArrayAdapter构造函数,您可以看到第二个参数,resource
,它需要包含文本视图的布局文件的资源ID,以便在实例化视图时使用。
。例如,您可以使用gson,也可以使用JSON对象和JS自己解析它当你解析它时,就像java/android中的其他模型一样使用它。
ArrayAdapter<Model>;
public class Producteur {
// Private attributes
private int mId;
// List of Produit, which is another model or object (call it however you want) created by you.
private List<Produit> mProduits;
private String mNom;
// Attributes getters and setters (they are optionnal in this example, but it's always good idea to have them)
public int get_mId() { return this.mId; }
public void set_mId(int id) { this.mId = id; }
public List<Produit> get_mProduits() { return this.mProduits; }
public void set_mProduits(List<Produit> produits) { this.mProduits = produits; }
public String get_mNom() { return this.mNom; }
public void set_mNom(String nom) { this.mNom= nom; }
// Constructor
public Producteur(int id, List<Produit> produits, String nom) {
this.mId = id;
this.mProduits = produits;
this.mNom = nom;
}
}
public class Produit {
// A product has two attributes: variete and recolte. I'll let you complete
// this class, you can inspire yourself of the above example.
}
// Instead of a hashmap, we'll be using a list of "Producteur"
List<Producteur> producteurs = new List<Producteur>();
JSONArray jarray=new JSONArray(resSel);
//String acteur_id="";
for(int i=0;i<jarray.length();i++){
JSONObject json = jarray.getJSONObject(i);
int id = json.getInt("acteurid");
// Note the name is not part of the "productions" JSONArray
String nom = json.getString("nom");
// Here we'll declare the productor's productions
List<Produit> productions = new List<Produit>();
JSONArray prodar=json.getJSONArray("productions");
for(int j=0;j<prodar.length();j++){
JSONObject prod=prodar.getJSONObject(j);
String variete = prod.getString("variete");
int recolte = Integer.parseInt(prod.getString("recolte"));
// We'll create a new "Produit" at each loop and add it to the productor's product list.
Produit unProduit = new Produit(variete, recolte);
productions.add(unProduit);
}
// We'll create a new productor at each loop and add it to the productors list
Producteur unProducteur = new Producteur(id, productions, nom);
producteurs.add(unProducteur);
}
ListView listprod = (ListView)findViewById(R.id.list);
// We will passe the list of "Producteur" we just created above as the objects to represent in the ListView
ArrayAdapter<Producteur> producteursAdapter = new ArrayAdapter<Producteur>(this, R.layout.list_item, producteurs);
listprod.setAdapter(producteursAdapter);