我在android中解析HAL + JSON对象时遇到问题。这是我的代码:
package com.example.philipp.productshopproject;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
;
/**
* Created by Philipp on 01.06.2016.
*/
public class GetProducts extends AsyncTask<String, Void, String> {
String result = "";
@Override
protected String doInBackground(String... urls) {
String url = urls[0];
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
// c.setConnectTimeout(timeout);
// c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
Log.d("kacka", sb.toString());
int i;
try {
JSONObject jsonObject = new JSONObject(sb.toString());
JSONArray jsonArray = jsonObject.getJSONArray("_embedded");
Log.d("kacka", jsonObject.toString());
for(i=0; i < jsonArray.length(); i++) {
JSONObject jObject = jsonArray.getJSONObject(i);
String produktname = jObject.getString("produktname");
String kategorie = jObject.getString("kategorie");
String beschreibung = jObject.optString("beschreibung").toString();
Double preis = jObject.getDouble("preis");
Double bewertung = jObject.getDouble("bewertung");
Log.d("kacka", beschreibung);
} // End Loop
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
return sb.toString();
}
} catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
这是HAL + JSON数据:
{
"_id": "products",
"_returned": 2,
"_embedded": {
"rh:doc": [
{
"_id": {
"$oid": "578b712226f1a7f670df2ae9"
},
"_etag": {
"$oid": "578b719e0106fa1728fdc730"
},
"produktname": "Badehaube",
"kategorie": "Sportartikel",
"preis": 25,
"beschreibung": "Badehaube",
"bewertung": 3,
"ratedBy": [
"admin"
],
...
当我开始这个时,我得到这个错误:
07-17 15:03:41.113 25811-25835 /? E / JSONException:错误: org.json.JSONException:值 {&#34; rh的信息:doc&#34;:[{&#34; _id&#34; {&#34; $ OID&#34;:&#34; 578b712226f1a7f670df2ae9&#34;}&#34; _etag&# 34;:{&#34; $ OID&#34;:&#34; 578b719e0106fa1728fdc730&#34;}&#34; produktname&#34;:&#34; Badehaube&#34;&#34; kategorie&#34; :&#34; Sportartikel&#34;&#34; PREIS&#34;:25,&#34; beschreibung&#34;:&#34; Badehaube&#34;&#34; .....
你知道如何解析这个吗?
答案 0 :(得分:0)
下面:
JSONArray jsonArray = jsonObject.getJSONArray("_embedded");
_embedded
是JSONObject
,其中包含rh:doc
JSONArray
。因此需要从rh:doc
获取JSONArray
_embedded
:
JSONObject _embeddedObject = jsonObject.getJSONObject("_embedded");
//rh:doc JSONArray
JSONObject rndocJSONArray = _embeddedObject.getJSONArray("rh:doc");