我從一個PHP文件,互聯網上獲得此JSON對象:如何將此JSON對象轉換爲字符串數組?
{
"1": "bar",
"2": "foo",
"3": "umIar",
"4": "shubham"
}
我嘗試了很多,但無法將其轉換爲String
陣列。怎麼做?
我用這個JAVA代碼:
public class showNewsFeed extends AsyncTask <String, Void, String> {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.ludlowcastle.co.in/getNotifiedDTU/newsFeed.php");
String result;
protected String doInBackground(String... voids){
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String result = convertStreamToString(is);
System.out.println("String response = " + result);
} catch (Exception e){
e.printStackTrace();
}
return result;
}
protected void onProgressUpdate(Void...voids){
}
protected void onPostExecute(String result){
super.onPostExecute(result);
try {
JSONObject obj=new JSONObject(result);
Iterator iter = obj.keys();
while(iter.hasNext()){
String key = (String)iter.next();
System.out.println(obj.getString(key));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
這是logcat的:
02-18 04:58:19.412: W/System.err(1268): java.lang.NullPointerException
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONObject.<init>(JSONObject.java:154)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONObject.<init>(JSONObject.java:171)
02-18 04:58:19.431: W/System.err(1268): at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:71)
02-18 04:58:19.431: W/System.err(1268): at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:1)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 04:58:19.451: W/System.err(1268): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 04:58:19.451: W/System.err(1268): at android.os.Looper.loop(Looper.java:137)
02-18 04:58:19.451: W/System.err(1268): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-18 04:58:19.451: W/System.err(1268): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 04:58:19.451: W/System.err(1268): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 04:58:19.451: W/System.err(1268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 04:58:19.461: W/System.err(1268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 04:58:19.471: W/System.err(1268): at dalvik.system.NativeStart.main(Native Method)
的可能重複:[{1}](HTTP:/ /stackoverflow.com/q/7551305/1438733),[{2}](http://stackoverflow.com/q/6356231/1438733),[{3}](http://stackoverflow.com/q/4604657/1438733)和[許多其他](https:// www .google.com /搜索?HL = EN&Q =機器人+ JSON +至+串+數組)。 – Eric 2013-02-18 04:45:52
add [this](http://stackoverflow.com/questions/4819405/json-formatted-string-to-string-array)和[this too](http://stackoverflow.com/questions/9708918/java-解析-json-string-to-array-or-objectlist) – SudoRahul 2013-02-18 04:49:23
只是替換這一行String result = convertStreamToString(is);結果= convertStreamToString(is);在後臺做的 – Khan 2013-02-18 05:05:41