2014-04-01 128 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與陣列和元素開始在其內部是在陣列。