-2

给定 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

4 回答 4

6

您可以使用GsonJSON库直接将字符串解析为 POJO。例如

gson.fromJson(yourJsonStr, yourPojoType);

然后根据需要使用 POJO。

于 2013-05-01T11:43:11.623 回答
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");
}

以同样的方式解析其他人。

于 2013-05-01T11:47:16.913 回答
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。如果你静态解析这个字符串,那么请像上面一样格式化字符串

于 2013-05-01T12:15:34.807 回答
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();
                        }
于 2013-05-01T11:51:31.483 回答