简体   繁体   English

json android:解析json

[英]json android: parsing json

I have a json : 我有一个json:

[ {user:"John",s:"Ldh",e:"usa"},{user:"Paul",s:"bukit panjang ",e:"FedExForum - Memphis"},{user:"ross",s:"bukit panjang ",e:"FedExForum - Memphis "}]

I am parsing this with the following code to retrieve all the values of "user" .. 我正在用下面的代码对此进行解析,以检索“ user”的所有值。

public class ListViewAndroidActivity extends ListActivity {
private String newString, user;
ArrayList<String> results = new ArrayList<String>();
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
TestServiceActivity test = new TestServiceActivity(); //This returns json from the server
   newString = test.readPooledFeed(); // returned json in string format

   JSONArray rootArray = new JSONArray(newString);
   int len = rootArray.length();
   for(int i = 0; i < len; ++i) {
       JSONObject obj = rootArray.getJSONObject(i);

      user = obj.optString("user");
    results.add(user);
   }
}

This gives me no error.. but nothing is shown on the screen .. kindly help! 这没有给我任何错误..但是屏幕上什么都没有显示..请帮助!

optString : Get an optional string associated with a key. optString:获取与键关联的可选字符串。

I don't see the key u in your JSON object, shouldn't it be user = obj.optString("user"); 我在您的JSON对象中看不到密钥u ,应该不是user = obj.optString("user");

JSON formatting JSON格式

Your JSON data is not valid, it's valid as javascript, but not as JSON. 您的JSON数据无效,它作为javascript有效,但不是JSON。

All properties should be quoted in JSON: 所有属性均应使用JSON引用:

[
 { "user": "John", "s": "Ldh", "e": "usa"},
 { "user":"Paul", "s":"bukit panjang ", "e": "FedExForum - Memphis" },
 { "user": "ross", "s":"bukit panjang ", "e":"FedExForum - Memphis "}
]

Indentation is not needed, I just did it to make it clearer. 缩进不是必需的,我只是做得更清楚。

Missing field 遗失领域

Your parser seems to ignore that your input data is invalid. 您的解析器似乎忽略了您的输入数据无效。 Other problem could be as others mentioned you're requesting the u property at user = obj.optString("u"); 其他问题可能是因为其他人提到您要在user = obj.optString("u");处请求u属性user = obj.optString("u"); which does not exists. 不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM