[英]JSON Object Parsing for Android
Basically I have this basic json file. 基本上我有这个基本的json文件。
{
"email":"theoaristi53@gmail.com",
"name":"Theo Tziomakas"
}
which is located it a server as it looks. 从外观上看它是一台服务器。 Now I want to get it from there back in Android phone. 现在,我想在Android手机中从那里得到它。
I use this snippet but I get a null for both email and name.
JSONObject jsonObject = new JSONObject(content);
Person p = new Person();
jsonObject.put("email",p.getEmail());
jsonObject.put("name",p.getName());
I am going to fetch the data from the server using the Volley libray. 我将使用Volley libray从服务器获取数据。
public class MainActivity extends ActionBarActivity {
public static final String BASE_URL =
"http://test.lovenaxos.com/testing.json";
public List<Person> personList;
public TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView)findViewById(R.id.text);
requestData("http://test.lovenaxos.com/testing.json");
}
public void requestData(String url){
StringRequest request = new StringRequest(url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
personList = JsonParser.parseFeed(response);
updateData();
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
protected void updateData(){
if(personList!=null){
for(Person p : personList){
output.append(p.getName() + "\n");
}
}
}
} }
Instead of put(..)
use getString(.)
: 代替put(..)
使用getString(.)
:
JSONObject jsonObject = new JSONObject(content);
String email = jsonObject.getString("email");
String name = jsonObject.getString("name");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.