我正在从URL解析数据,其得到下面提到的错误.
原始数据从Server.Iot能够使用Json解析分割数据完美显示.
请帮我解决这个错误
编辑:1
来自URL的Json响应
[ { "ID": 4, "Name": "Vinoth", "Contact": "1111111111", "Msg": "1" }, { "ID": 5, "Name": "Mani", "Contact": "22222222", "Msg": "1" }, { "ID": 6, "Name": "Manoj", "Contact": "33333333333", "Msg": "1" } ]
错误:
org.json.JSONException: Value [{"ID":1,"Name":"Lalita","Contact":"9997162499","Msg":"1"},{"ID":2,"Name":"kumar","Contact":"123456789","Msg":"1"}] of type java.lang.String cannot be converted to JSONArray 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at org.json.JSONArray.(JSONArray.java:96) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at org.json.JSONArray. (JSONArray.java:108) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at com.knowledgeflex.restapidemo.MainActivity$LoadService.onPostExecute(MainActivity.java:135) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at com.knowledgeflex.restapidemo.MainActivity$LoadService.onPostExecute(MainActivity.java:58) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.os.Looper.loop(Looper.java:136) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5584) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at java.lang.reflect.Method.invoke(Method.java:515) 12-11 18:23:27.249 30195-30195/com.knowledgeflex.restapidemo W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 12-11 18:23:27.259 30195-30195/com.knowledgeflex.restapidemo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 12-11 18:23:27.259 30195-30195/com.knowledgeflex.restapidemo W/System.err: at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
public class MainActivity extends Activity { TextView name1,email,status,face; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button GetServerData = (Button) findViewById(R.id.button1); name1 = (TextView)findViewById(R.id.sname); email = (TextView)findViewById(R.id.email); status = (TextView)findViewById(R.id.status); face = (TextView)findViewById(R.id.fb); GetServerData.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Server Request URL String serverURL = "http://webapp/api/values"; // Create Object and call AsyncTask execute Method new LoadService().execute(serverURL); } }); } // Class with extends AsyncTask class private class LoadService extends AsyncTask{ private final HttpClient Client = new DefaultHttpClient(); private String Content; private String Error = null; private final String TAG = null; String name = null; private ProgressDialog Dialog = new ProgressDialog(MainActivity.this); TextView uiUpdate = (TextView) findViewById(R.id.textView2); protected void onPreExecute() { // NOTE: You can call UI Element here. // UI Element uiUpdate.setText(""); Dialog.setMessage("Loading service.."); Dialog.show(); } // Call after onPreExecute method protected Void doInBackground(String... urls) { try { // NOTE: Don't call UI Element here. HttpGet httpget = new HttpGet(urls[0]); ResponseHandler responseHandler = new BasicResponseHandler(); Content = Client.execute(httpget, responseHandler); } catch (ClientProtocolException e) { Error = e.getMessage(); cancel(true); } catch (IOException e) { Error = e.getMessage(); cancel(true); } return null; } protected void onPostExecute(Void unused) { // Close progress dialog Dialog.dismiss(); Log.e(TAG, "------------------------------------- Output: " + Content); try { JSONArray jArr=new JSONArray(Content); for(int i=0;i
Hits.. 10
根据您的响应,JSONArray和gson库最好在json数据解析时使用,因此在类下面使用任何类型的数据
import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class ApiData { @SerializedName("data") @Expose private JsonArray Data; publicList getData(Class c) { Type type = new ListParams(c); return new Gson().fromJson(Data, type); } private class ListParams implements ParameterizedType { private Type type; private ListParams(Type type) { this.type = type; } @Override public Type[] getActualTypeArguments() { return new Type[]{type}; } @Override public Type getRawType() { return ArrayList.class; } @Override public Type getOwnerType() { return null; } @Override public boolean equals(Object o) { return super.equals(o); } } } 创建模型类,如:
public class Model{ String ID; String Name; String Contact; String msg; }现在解析您的数据,如:
ApiData apiData = new Gson().fromJson(Content, ApiData.class); Lismodels = apiData.getData(Model.class);
如果您遇到任何问题或需要解释,请告诉我 (2认同)
Boopathi.. 6
try { Object jsonObject = new JSONTokener(Content).nextValue(); JSONArray jArr=new JSONArray(jsonObject ); for(int i=0;i直接你不能将字符串应用于数组,你应该将字符串转换为jsonobject,然后你可以做对象到数组.希望你能理解
1> Hits..:根据您的响应,JSONArray和gson库最好在json数据解析时使用,因此在类下面使用任何类型的数据
import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class ApiData { @SerializedName("data") @Expose private JsonArray Data; publicList getData(Class c) { Type type = new ListParams(c); return new Gson().fromJson(Data, type); } private class ListParams implements ParameterizedType { private Type type; private ListParams(Type type) { this.type = type; } @Override public Type[] getActualTypeArguments() { return new Type[]{type}; } @Override public Type getRawType() { return ArrayList.class; } @Override public Type getOwnerType() { return null; } @Override public boolean equals(Object o) { return super.equals(o); } } } 创建模型类,如:
public class Model{ String ID; String Name; String Contact; String msg; }现在解析您的数据,如:
ApiData apiData = new Gson().fromJson(Content, ApiData.class); Lismodels = apiData.getData(Model.class);
如果您遇到任何问题或需要解释,请告诉我
2> Boopathi..:try { Object jsonObject = new JSONTokener(Content).nextValue(); JSONArray jArr=new JSONArray(jsonObject ); for(int i=0;i直接你不能将字符串应用于数组,你应该将字符串转换为jsonobject,然后你可以做对象到数组.希望你能理解
3> AndiGeeky..:因为我在这里添加了转义到你的json只是暂时存储它:
请检查下面的解析代码,它对我有用:
String response = "[\r\n {\r\n \"ID\": 4,\r\n \"Name\": \"Vinoth\",\r\n \"Contact\": \"1111111111\",\r\n \"Msg\": \"1\"\r\n },\r\n {\r\n \"ID\": 5,\r\n \"Name\": \"Mani\",\r\n \"Contact\": \"22222222\",\r\n \"Msg\": \"1\"\r\n },\r\n {\r\n \"ID\": 6,\r\n \"Name\": \"Manoj\",\r\n \"Contact\": \"33333333333\",\r\n \"Msg\": \"1\"\r\n }\r\n]"; try { JSONArray jsonArray = new JSONArray(response); // replace response with your response string for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); Log.e("ID", jsonObject.getInt("ID") + ""); Log.e("Name", jsonObject.getString("Name")); Log.e("Contact", jsonObject.getString("Contact")); Log.e("Msg", jsonObject.getString("Msg")); } } catch (JSONException e) { e.printStackTrace(); }我打印的日志:
12-17 15:42:54.459 9064-9064/com.example.testapplication E/ID:4 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Name:Vinoth 12-17 15:42 :54.459 9064-9064/com.example.testapplication E /联系方式:1111111111 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Msg:1 12-17 15:42:54.459 9064-9064/com.example.testapplication E/ID:5 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Name:Mani 12-17 15:42:54.459 9064-9064/com.example.testapplication E /联系方式:22222222 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Msg:1 12-17 15:42:54.459 9064-9064/com.example.testapplication E/ID:6 12- 17 15:42:54.459 9064-9064/com.example.testapplication E/Name:Manoj 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Contact:33333333333 12-17 15:42:54.459 9064-9064/com.example.testapplication E/Msg:1
谢谢 ..!