如何在Android中获取Json字符串?

时间:2015-04-23 14:50:02

标签: android

它显示没有找到数据,我在哪里可以获取JSON字符串但无法获取每个对象?

{"layout_bg1":"#23a9e6","layout_bg2":"#024E99","layout_bg3":"#EA7116","service_name":"#003366","font_color1":"#333333","font_color2":"#666666","icon_1":"tabPreferences/federal.jpg","icon_2":"tabPreferences/logo_federal_whitebg.png"}`

> 04-23 20:14:53.395  24612-24637/banks.noq.com.noq E/Error﹕ No value
> for null 04-23 20:14:53.395  24612-24637/banks.noq.com.noq
> W/System.err﹕ org.json.JSONException: No value for null 04-23
> 20:14:53.395  24612-24637/banks.noq.com.noq W/System.err﹕ at
> org.json.JSONObject.get(JSONObject.java:354) 04-23 20:14:53.405 
> 24612-24637/banks.noq.com.noq W/System.err﹕ at
> org.json.JSONObject.getJSONObject(JSONObject.java:569) 04-23
> 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> banks.noq.com.noq.MainActivity$TabPreferences.doInBackground(MainActivity.java:472)
> 04-23 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> banks.noq.com.noq.MainActivity$TabPreferences.doInBackground(MainActivity.java:446)
> 04-23 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> android.os.AsyncTask$2.call(AsyncTask.java:287) 04-23 20:14:53.405 
> 24612-24637/banks.noq.com.noq W/System.err﹕ at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
> 04-23 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> java.util.concurrent.FutureTask.run(FutureTask.java:137) 04-23
> 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 04-23
> 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
> 04-23 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
> 04-23 20:14:53.405  24612-24637/banks.noq.com.noq W/System.err﹕ at
> java.lang.Thread.run(Thread.java:856) 04-23 20:14:53.435 
> 24612-24612/banks.noq.com.noq W/System.err﹕ org.json.JSONException: No
> value for layout_bg1 04-23 20:14:53.435  24612-24612/banks.noq.com.noq
> W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354) 04-23
> 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> org.json.JSONObject.getString(JSONObject.java:510) 04-23 20:14:53.435 
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> banks.noq.com.noq.MainActivity$TabPreferences.onPostExecute(MainActivity.java:502)
> 04-23 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> banks.noq.com.noq.MainActivity$TabPreferences.onPostExecute(MainActivity.java:446)
> 04-23 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.os.AsyncTask.finish(AsyncTask.java:631) 04-23 20:14:53.435 
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.os.AsyncTask.access$600(AsyncTask.java:177) 04-23 20:14:53.435
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
> 04-23 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.os.Handler.dispatchMessage(Handler.java:99) 04-23 20:14:53.435
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.os.Looper.loop(Looper.java:137) 04-23 20:14:53.435 
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> android.app.ActivityThread.main(ActivityThread.java:4740) 04-23
> 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> java.lang.reflect.Method.invokeNative(Native Method) 04-23
> 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> java.lang.reflect.Method.invoke(Method.java:511) 04-23 20:14:53.435 
> 24612-24612/banks.noq.com.noq W/System.err﹕ at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
> 04-23 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 04-23
> 20:14:53.435  24612-24612/banks.noq.com.noq W/System.err﹕ at
> dalvik.system.NativeStart.main(Native Method) 04-23 20:15:08.985 
> 24730-24730/banks.noq.com.noq I/dalvikvm﹕ Could not find method
> android.view.ViewGroup.onNestedScrollAccepted, referenced from me

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

你的JSON:

{
  "layout_bg1": "#23a9e6",
  "layout_bg2": "#024E99",
  "layout_bg3": "#EA7116",
  "service_name": "#003366",
  "font_color1": "#333333",
  "font_color2": "#666666",
  "icon_1": "tabPreferences/federal.jpg",
  "icon_2": "tabPreferences/logo_federal_whitebg.png"
}
try{
  JSONObject data = new JSONObject( new String("your json") );
  String layout_bg1 = data.getString("layout_bg1");
  //etc..etc..

}catch(JSONException e){
   //
}