C#字典json数据到java类对象
我有一个json数据,比如C#字典json数据到java类对象,c#,android,json,C#,Android,Json,我有一个json数据,比如 { "1": "AAAA", "2": "BBBB","3":"CCCC","4":"DDDD","5":"EEEE"} 它由c#dictionary构建,并向android客户端发送响应。我在java上类似的课 public class Customer { int customerId; String customerName; } 如何在java中解析或为此类分配json值这将完成以下工作: String json = "{ \"1\" :
{ "1": "AAAA", "2": "BBBB","3":"CCCC","4":"DDDD","5":"EEEE"}
它由c#dictionary构建,并向android客户端发送响应。我在java上类似的课
public class Customer {
int customerId;
String customerName;
}
如何在java中解析或为此类分配json值这将完成以下工作:
String json = "{ \"1\" : \"AAA\", \"2\" : \"BBB\" }";
JSONObject object = null;
try {
object = new JSONObject(json);
} catch (JSONException e1) {
e1.printStackTrace();
}
Iterator<String> i = (Iterator<String>)object.keys();
ArrayList<Customer> customers = new ArrayList<Customer>();
while(i.hasNext()) {
String key = i.next();
String name = null;
try {
name = object.getString(key);
} catch (JSONException e) {
e.printStackTrace();
}
Customer c = new Customer();
c.customerId = Integer.parseInt(key);
c.customerName = name;
customers.add(c);
}
String json=“{\'1\':\'AAA\',\'2\':\'BBB\'”;
JSONObject对象=null;
试一试{
对象=新的JSONObject(json);
}捕获(JSONException e1){
e1.printStackTrace();
}
迭代器i=(迭代器)object.keys();
ArrayList客户=新的ArrayList();
while(i.hasNext()){
字符串键=i.next();
字符串名称=null;
试一试{
name=object.getString(键);
}捕获(JSONException e){
e、 printStackTrace();
}
客户c=新客户();
c、 customerId=Integer.parseInt(键);
c、 客户名称=名称;
加上(c);
}