オブジェクト?誰かが私にこのための良いJSONライブラリを提案できますか? JarをAndroidで使用できますか?解析シンプルなJSONと負荷が、私はこの単純なクラスを持っている
0
A
答えて
1
これはとても簡単です。完全なコードはここにあります
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public List<element> generateList()
{
String jsonString = "[{\"id\": 1,\"name\": \"water\"},{\"id\": 2,\"name\": \"fire\"}]";
JSONArray json = null;
List<element> mElementList = new ArrayList<element>();
try {
json = new JSONArray(jsonString);
} catch (JSONException je) {
Log.e("TAG", "Json Exception" + je.getMessage());
return;
}
JSONObject jsonObject = null;
element ele = null;
for (int i = 0; i < json.length(); i++) {
try {
jsonObject = json.getJSONObject(i);
ele = new element();
if(jsonObject.has("id"))
{
ele.id = jsonObject.getString("id")
}
if(jsonObject.has("name"))
{
ele.name = jsonObject.getString("name")
}
mElementList.add(ele);
} catch (JSONException jee) {
Log.e("TAG", "" + jee.getMessage());
}
}
return mElementList;
}
2
Try Jackson;それはこれ以上のことを処理することができます。
3
また、あなたは使用することができますあなたの場合は、Androidでorg.json
ライブラリに組み込まれて使用することができます。
List<Element> elements = new LinkedList<Element>();
JSONArray arr = new JSONArray(jsonString);
JSONObject tempObj;
Element tempEl;
for(int i = 0; i < arr.length(); i++){
tempObj = arr.getJSONObject(i);
tempEl = new Element();
tempEl.id = tempObj.getInt("id");
tempEl.name = tempObj.getString("name");
elements.add(tempEl);
}
そして、あなたは、要素のリストを取得します。
tempEl.idを2回設定しました! –
aww申し訳ありませんが、今修正されました。 – ayublin