0

I have the following

  JSONObect {"28":["Mugisha","Christian"]} 

from a larger JSONArray that I need to be able to translate

 {"28":["Mugisha","Christian"]}

into an object which has

int itemId;
String firstName;
String lastName;

How do I go about this?

4

4 回答 4

2

不要自己解析!使用内置的 JSONObject 类。

JSONObject object = new JSONObject(jsonString);

然后,除非您已经在某处拥有 id,否则请执行以下操作:

JSONArray names = object.names();
int id = Integer.parseInt(names.get(0));
JSONArray contents = object.getJSONArray(names.get(0));
String firstName = contents.get(0);
String lastName = contents.get(1);

简单的。如果您想获得多个名称,只需遍历名称即可。

于 2013-05-27T09:18:53.160 回答
1

你必须像这样解析

JSONObject job=new JSONObject('your json string');
Iterator itr=job.keys();
while(itr.hasNext())
{
int id=Integer.parseInt(itr.next());
JSONArray jar=job.getJSONArray(id);
String firstName=jar.getString(0);
String lastName=jar.getString(1);
}
于 2013-05-27T09:22:32.073 回答
0

这样做:

    JSONObject json = new JSONObject(result);

    String usuario= json.getString("usuario");

    int idperon = json.getInt("idperson");

    String nombre = json.getString("nombre");
于 2013-05-27T09:17:45.897 回答
0
  1. 你需要了解的格式JSON
  2. 你需要学习SerializationDeserialization
  3. 您需要类似GSON库之类的东西来将其来回解析为 JSON

使用GSON,就像使用fromJson(String,Class)将其转换为POJO(普通旧 Java 对象)一样简单 :)

关键是要了解如何将 JSON 类映射到 Java 类。我相信你会找到很多关于它的教程:)

于 2013-05-27T09:16:03.583 回答