如何在Android开发中解析JSON数组

时间:2013-05-01 11:35:09

标签: android json

给定JSON字符串:

[
   {
    "id": "236",
    "fbid": "",
    "fbpw": "",
    "udid": "1400000210033",
    "state": "",
    "fullname": "",
    "house": "",
    "office": "",
    "mobile": "",
    "phone": "",
    "email1": "prabhjotkaur3@hotmail.com",
    "email2": "",
    "email3": "",
    "descript": "",
    "facebook": "",
    "twitter": "",
    "gplus": "",
    "youtube": "",
    "linkedin": "",
    "tumblr": "",
    "instagram": "",
    "lasttime": "2013-05-01 20:30:05"
    }
]

我想解析这段代码,但没有得到如何解析。请建议我如何解析这个。

提前致谢!

4 个答案:

答案 0 :(得分:6)

您可以使用Gson库直接将JSON字符串解析为POJO。 对于例如

gson.fromJson(yourJsonStr, yourPojoType);

然后根据需要使用POJO。

答案 1 :(得分:4)

result="[{ "id":236, "fbid":"", "udid":1400 }]"

JSONArray jArray = new JSONArray(result);                       
for (int i = 0; i < jArray.length(); i++)
{
JSONObject jObject = jArray.getJSONObject(i);
String id = jObject.getString("id");
String fbid = jObject.getString("fbid");
String udid = jObject.getString("udid");
}

用同样的方式解析别人。

答案 2 :(得分:1)

你可以像这样解析这个json:

                        try {
                            String JsonString = "";
                            JSONArray mJsonArray = new JSONArray(JsonString);
                            for (int i = 0; i < mJsonArray.length(); i++) {

                            JSONObject mJsonObject = mJsonArray.getJSONObject(i);

//                            Get you data in variables
                            String mID = mJsonObject.getString("id");
                            String mFBID = mJsonObject.getString("fbid");
                            String mFBPW = mJsonObject.getString("fbpw");
                            String mUDID = mJsonObject.getString("udid");
                            String mSTATE = mJsonObject.getString("state");
                            .....
                            }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

答案 3 :(得分:1)

public class MainActivity extends Activity {

    String[] id,fbid,fbpw,udid,state;
    String JsonString ="[{\"id\": \"236\",\"fbid\": \"123\",\"fbpw\": \"567\",\"udid\":\"1400000210033\",\"state\": \"gujarat\"}]";
    JSONArray j1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            j1= new JSONArray(JsonString);
            id=new String[j1.length()];
            fbid=new String[j1.length()];
            fbpw=new String[j1.length()];
            udid=new String[j1.length()];
            state=new String[j1.length()];
            for(int i=0;i<j1.length();i++)
            {
                id[i]=j1.getJSONObject(i).getString("id");
                Log.e("id",id[i]);
                fbid[i]=j1.getJSONObject(i).getString("fbid");
                Log.e("fbid",fbid[i]);
                fbpw[i]=j1.getJSONObject(i).getString("fbpw");
                Log.e("fbpw",fbpw[i]);
                udid[i]=j1.getJSONObject(i).getString("udid");
                Log.e("udid",udid[i]);
                state[i]=j1.getJSONObject(i).getString("state");
                Log.e("state",state[i]);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

试试这个对你有用。如果你从url获取json数据所以请将它存储在字符串变量中并传递给它j1 jsonArray.if你静态解析这个字符串所以请求格式化上面的字符串确定