Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中循环浏览json数据_Java_Android_Json_Jsonp - Fatal编程技术网

Java 如何在android中循环浏览json数据

Java 如何在android中循环浏览json数据,java,android,json,jsonp,Java,Android,Json,Jsonp,如标题所述。如何循环从服务器获取的json数据。我得到了这种json数据 { "tag":"home", "success":1, "error":0, "uid":"4fc8f94f1a51c5.32653037", "name":"Saleem", "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg", "places": {

如标题所述。如何循环从服务器获取的json数据。我得到了这种json数据

{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Saleem",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Name",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Name",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
 public class Home extends Activity {
Button btnLogout;
ScrollView svHome;
UserFunctions userFunctions;
LoginActivity userid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    userid = new LoginActivity();
    svHome = (ScrollView)findViewById(R.id.svHome);
    setContentView(R.layout.home);

    userFunctions = new UserFunctions();

    /***********************************************************/
            //here is where my above mentioned json data is
    JSONObject json = userFunctions.homeData();

    try {
        if(json != null && json.getString("success") != null) {
            //login_error.setText("");
            String res = json.getString("success");
            //userid = json.getString("uid").toString();
            if(Integer.parseInt(res) == 1) {
                                    //currently this only shows the first json object
                Log.e("pla", json.toString());
            } else {
                //login_error.setText(json.getString("error_msg"));
            }
        } else {
            Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /*******************************************************/
}

}
这里是我获取json数据的地方

{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Saleem",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Name",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
{
   "tag":"home",
   "success":1,
   "error":0,
   "uid":"4fc8f94f1a51c5.32653037",
   "name":"Name",
   "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
   "places":
   {
       "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
       "created_at":"2012-06-02 00:00:00",
       "seeked":"0"
    }
}
 public class Home extends Activity {
Button btnLogout;
ScrollView svHome;
UserFunctions userFunctions;
LoginActivity userid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    userid = new LoginActivity();
    svHome = (ScrollView)findViewById(R.id.svHome);
    setContentView(R.layout.home);

    userFunctions = new UserFunctions();

    /***********************************************************/
            //here is where my above mentioned json data is
    JSONObject json = userFunctions.homeData();

    try {
        if(json != null && json.getString("success") != null) {
            //login_error.setText("");
            String res = json.getString("success");
            //userid = json.getString("uid").toString();
            if(Integer.parseInt(res) == 1) {
                                    //currently this only shows the first json object
                Log.e("pla", json.toString());
            } else {
                //login_error.setText(json.getString("error_msg"));
            }
        } else {
            Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /*******************************************************/
}

}
更新

根据答案中给出的链接进行更改后。这是我的零钱

 /***********************************************************/
    JSONObject json = userFunctions.homeData();
    String jsonData = json.toString();

    try {
        if(json != null && json.getString("success") != null) {
            //login_error.setText("");
            String res = json.getString("success");
            //userid = json.getString("uid").toString();
            if(Integer.parseInt(res) == 1) {
                JSONArray jsonArray = new JSONArray(jsonData);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Log.e("Object", jsonObject.getString("places"));
                    //Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
                }
                Log.e("pla", json.toString());
            } else {
                //login_error.setText(json.getString("error_msg"));
            }
        } else {
            Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
/***********************************************************/
JSONObject json=userFunctions.homeData();
字符串jsonData=json.toString();
试一试{
if(json!=null&&json.getString(“成功”)!=null){
//登录错误。setText(“”);
String res=json.getString(“成功”);
//userid=json.getString(“uid”).toString();
if(Integer.parseInt(res)==1){
JSONArray JSONArray=新的JSONArray(jsonData);
for(int i=0;i
您可以使用json库,如下所示:

导入org.json.JSONArray; 导入org.json.JSONObject

它允许您将json数据读入数组,如下所示:

JSONArray JSONArray=新的JSONArray([您的json数据])


尝试本教程:

请查看链接

如果可能的话,可以在json中进行更改,因为json中没有数组大括号“[”“]”,您需要它在循环中进行itrate

json应该是这样的

{
"arrayKey": [
    {
        "tag": "home",
        "success": 1,
        "error": 0,
        "uid": "4fc8f94f1a51c5.32653037",
        "name": "Saleem",
        "profile_photo": "http://example.info/android/profile_photos/profile1.jpg",
        "places": {
            "place_photo": "http://example.info/android/places_photos/place1.jpg",
            "created_at": "2012-06-02 00:00:00",
            "seeked": "0"
        }
    },
    {
        "tag": "home",
        "success": 1,
        "error": 0,
        "uid": "4fc8f94f1a51c5.32653037",
        "name": "Saleem",
        "profile_photo": "http://example.info/android/profile_photos/profile1.jpg",
        "places": {
            "place_photo": "http://example.info/android/places_photos/place1.jpg",
            "created_at": "2012-06-02 00:00:00",
            "seeked": "0"
        }
    }
]

}

正如日志猫所述,您正在尝试将JSONObject转换为JSONArray:

 org.json.JSONException: Value {"uid":"4fc8f94f1a51c5.32653037","places":{"place_photo":"http://example.info/android/places_photos/place1.jpg","created_at":"2012-06-02 00:00:00","seeked":"0","longitude":"24.943514","latitude":"60.167112"},"error":0,"success":1,"tag":"home","profile_photo":"http://example.info/android/profile_photos/profile1.jpg","name":"Zafar Saleem"} of type org.json.JSONObject cannot be converted to JSONArray

试着调试代码-找出抛出异常的位置,并在那里生成一个JSONObject而不是JSONArray。

我推荐的另一种方法是使用库,它很容易结束,不会带来痛苦。对于格式良好的Json

您可以在Json中进行更改吗?因为json中没有大括号“[”“]”的数组?请参阅我上面的更新。根据上面给出的链接进行更改后,我得到了一个logcat。您可以在json中进行更改吗?由于json中没有大括号“[”“]”数组,您能给我一些链接,让我可以将json更改为您刚才提到的格式吗?感谢您,我认为json来自服务器………如果是,则取决于服务器端语言……我在服务器端使用php创建对象数组,然后在json数组中转换