我无法解析为什么在我的android项目中解析JSON(如“ JSONObject无法转换为JSONArray”)时显示异常

Android开发人员

这是我卡住的代码:JSONObject jsonObj = new JSONObject(jsonStr); 常见问题= jsonObj.getJSONArray(TAG_FAQ);

                for (int i = 0; i < Faq.length(); i++) {
                    JSONObject c = Faq.getJSONObject(i);

                    String questions = c
                            .getString("TAG_QUESTIONS");
                    String answer = c.getString("TAG_ANSWER");
                    HashMap<String, String> Faq = new HashMap<String, String>();
                    faqList.add(Faq);

                }

谢谢您的所有重播,请检查上面的代码...

玛哈德夫(Mahadev Dalavi)
        [
            {
                "id": 1,
                "creationDate": null,
                "updationDate": null,
                "creationUserId": null,
                "updation‌​UserId": null,
                "questions": "What is Android",
                "answer": "Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google. ",
                "deactivated": false
            }
        ]
        //this your json array i parse in following format
        package com.example.jsondemo;

        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;


        public class MainActivity extends ActionBarActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                String jsonobj="[{\"id\":1,\"creationDate\":null,\"updationDate\":null,\"creationUserId\":null,\"updationUserId\":null,\"questions\":\"What is Android\",\"answer\":\"Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google.\",\"deactivated\":false}]";

// yuor json is correct. don't confuse about above json string jsut I created    //json string for your understanding

                try {
                    JSONArray ja=new JSONArray(jsonobj);

                    for (int count = 0; count < ja.length(); count++)

                     {

                     JSONObject obj = ja.getJSONObject(count);

                    int ID = obj.getInt("id");
                    String CreationDate=obj.getString("creationDate");
                    String UpdationDate=obj.getString("updationDate");
                    String CreationUserId=obj.getString("creationUserId");
                    String Questions=obj.getString("questions");
                    String Answer = obj.getString("answer");
                    boolean Deactivated=obj.getBoolean("deactivated");
                    Log.d("test", "user id"+" "+ID);
                    Log.d("test", CreationDate);
                    Log.d("test", UpdationDate);
                    Log.d("test", CreationUserId);
                    Log.d("test", Questions);
                    Log.d("test", Answer);
                    Log.d("test","check"+ " "+Deactivated);

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


        }
        //please check logcat test value it sucessfull work

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档