2014-04-01 129 views
0

我有一个Android应用程序,它获取json字符串。Android和json解析

[ 
{ 
    "apellidos": "seguro", 
    "edad": 2, 
    "idninos": 1, 
    "nombre": "antonio", 
    "padres": "n1" 
}, 
{ 
    "apellidos": "martinez", 
    "edad": 2, 
    "idninos": 2, 
    "nombre": "sebastian", 
    "padres": "n2" 
}, 
{ 
    "apellidos": "lopez", 
    "edad": 2, 
    "idninos": 3, 
    "nombre": "francisaca", 
    "padres": "n3" 
}, 
{ 
    "apellidos": "maroto", 
    "edad": 2, 
    "idninos": 4, 
    "nombre": "gregoria", 
    "padres": "n4" 
}, 
{ 
    "apellidos": "maroto", 
    "edad": 2, 
    "idninos": 5, 
    "nombre": "javier", 
    "padres": "n4" 
}, 
{ 
    "apellidos": "ramirez", 
    "edad": 1, 
    "idninos": 6, 
    "nombre": "andrea", 
    "padres": "n5" 
}, 
{ 
    "apellidos": "leal", 
    "edad": 1, 
    "idninos": 7, 
    "nombre": "jorge", 
    "padres": "n6" 
}, 
{ 
    "apellidos": "rodriguez", 
    "edad": 2, 
    "idninos": 8, 
    "nombre": "antonio", 
    "padres": "n7" 
}, 
{ 
    "apellidos": "gomez", 
    "edad": 2, 
    "idninos": 9, 
    "nombre": "jaime", 
    "padres": "n8" 
}, 
{ 
    "apellidos": "jimenez", 
    "edad": 2, 
    "idninos": 10, 
    "nombre": "nerea", 
    "padres": "n9" 
}, 
{ 
    "apellidos": "gutierrez", 
    "edad": 2, 
    "idninos": 11, 
    "nombre": "paquito", 
    "padres": "n12" 
}, 
{ 
    "apellidos": "esteso", 
    "edad": 2, 
    "idninos": 12, 
    "nombre": "tomas", 
    "padres": "n15" 
} 
] 

我以这种方式显示:我在listview显示;

public class MainActivity extends ListActivity { 
private static final String LOGTAG = "LogsAndroid"; 
static String id; 
    EditText eTetx1; 
//contexto 
private Context context; 
//url 
private static String  

    url="http://192.168.1.33:8080/WSBaby1/webresources/com.gleegoo.ws.ninos/"; 
//variable de json 
private static final String TAG_NOMBRE="nombre"; 
private static final String TAG_APELLIDO="apellidos"; 
//lista arrays desplegables 
ArrayList<HashMap<String,String>> jsonlist= new ArrayList<HashMap<String,String>>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //llamando la actividad 
    new GetJSONActivity(MainActivity.this).execute(); 
    eTetx1 = (EditText) findViewById(R.id.editText1); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
private class GetJSONActivity extends AsyncTask<String, Void, String>{ 
    private ListActivity activity; 


    public GetJSONActivity(ListActivity activity){ 
     this.activity=activity; 
     context = activity; 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     JSONParser jParser = new JSONParser(); 
     //getting JSON string from url 
     JSONArray json = jParser.GetJSONfromUrl(url); 
     for (int i = 0; i < json.length(); i++) { 
      try { 
       JSONObject c = json.getJSONObject(i); 
       String vpeli=c.getString(TAG_NOMBRE); 
       String apell=c.getString(TAG_APELLIDO); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       // 
       map.put(TAG_NOMBRE, vpeli); 
       map.put(TAG_APELLIDO, apell); 
       jsonlist.add(map); 

      } catch (JSONException e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
       return "Error creando variables"; 
      } 
     } 
     return "EXITO"; 
    } 

    @Override 
    protected void onPostExecute(String success){ 
     ListAdapter adapter = new SimpleAdapter(context, jsonlist,R.layout.items, new String[] {TAG_NOMBRE,TAG_APELLIDO}, new int[]{R.id.nombre,R.id.tipo}); 
    setListAdapter(adapter); 
    //selecting single listview 
    getListView(); 
    } 

那么,这完美的作品,我的问题是这样的: 如果我要显示,例如从一个ID的所有数据, idnino

如3 {"apellidos":"lopez","edad":2,"idninos":3,"nombre":"francisaca","padres":"n3"}

他原以为有关更改网址,并把这个:

private static String url="http://192.168.1.33:8080/WSBaby1/webresources/com.gleegoo.ws.ninos/3 

乙UT我得到它,但不能表现出来.. 这是我的logcat:

04-01 12:43:51.165: E/JSON Parser(17792): Error traduciendo  
datosorg.json.JSONException: Value 
{"nombre":"francisaca","padres":"n3","idninos":3,"apellidos":"lopez","edad":2} of type 
org.json.JSONObject cannot be converted to JSONArray 
04-01 12:43:51.175: W/dalvikvm(17792): threadid=12: thread exiting with uncaught  
exception (group=0x419612a0) 
04-01 12:43:51.240: E/AndroidRuntime(17792): FATAL EXCEPTION: AsyncTask #1 
04-01 12:43:51.240: E/AndroidRuntime(17792): java.lang.RuntimeException: An error 
occured while executing doInBackground() 
04-01 12:43:51.240: E/AndroidRuntime(17792): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at   
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.util.concurrent.FutureTask.run(FutureTask.java:137) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.lang.Thread.run(Thread.java:856) 
04-01 12:43:51.240: E/AndroidRuntime(17792): Caused by: java.lang.NullPointerException 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
com.example.jsonprueba.MainActivity$GetJSONActivity.doInBackground(MainActivity.java:65) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
com.example.jsonprueba.MainActivity$GetJSONActivity.doInBackground(MainActivity.java:1) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
android.os.AsyncTask$2.call(AsyncTask.java:287) 
04-01 12:43:51.240: E/AndroidRuntime(17792): at 
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
04-01 12:43:51.240: E/AndroidRuntime(17792): ... 5 more 
+0

@anuruddhika请不要在您的编辑中加入任意词。 –

+0

你可以参考这[post](http://stackoverflow.com/questions/12312102/how-to-json-parsing-in-android) – Spinning

+0

你试图解析JSONObject到JSONArray。 {“apellidos”:“lopez”,“edad”:2,“idninos”:3,“nombre”:“francisaca”,“padres”:“n3”}这是JSONObject而不是JSONArray。 – Yuvaraja

回答

2

这是一个JSONArray而不是JSONObject - 实现从它JSONObject,使用

JSONObject jsonObject = jsonArray.getJSONObject(0); 

这得到这个JSONArray的第一个JSONObject。

如果您有多个一个JSONObjects,使用此:

JSONObject jsonObject; 
for(int n = 0; n < jsonArray.length(); n++) 
{ 
    jsonObject = jsonArray.getJSONObject(n); 
} 

为了获取值:

jsonObject.getString("name"); 
+0

感谢您的回复,但我无法完成工作。你能帮我修改doInBackground的方法吗? –

+0

老兄它是如此简单只需要一个for循环,你已经采取然后添加此JSONObject jsonObject = jsonArray.getJSONObject(0);在for循环中传递getJSONObject(int值)中的int循环的值,然后在每个这里使用我的jsonObject循环只是尝试它的家伙 –

2

您正试图解析JSON作为数组

JSONArray json = jParser.GetJSONfromUrl(url); 

但你的JSON是一个对象

{ 
    "apellidos": "lopez", 
    "edad": 2, 
    "idninos": 3, 
    "nombre": "francisaca", 
    "padres": "n3" 
} 

您的JSON应该是一个对象的数组只

[{ 
    "apellidos": "lopez", 
    "edad": 2, 
    "idninos": 3, 
    "nombre": "francisaca", 
    "padres": "n3" 
}] 

你可以尝试检查结果对象是一个数组或类似这样的

Object jsonResultObject = jParser.GetJSONfromUrl(url); 

if (jsonResultObject!=null) { 
    if (jsonResultObject instanceof JSONArray) { 
     ...... 
    }else if (jsonResultObject instanceof JSONObject) { 
     ..... 
    }else{ 
     throw new Exception("The result object is not a JSON object or an Array"); 
    } 

} 
1
对象

从编码中删除阵列并用对象做,因为你展示的JSON是对象而不是数组。

阵列开始与[] 对象与{}

你的JSON与阵列和元素开始在其内部是在阵列。