简体   繁体   English

Android解析HAL + JSON

[英]Android parse HAL+JSON

I have a problem parsing a HAL+JSON Object in android. 我在android中解析HAL + JSON对象时遇到问题。 This is my code: 这是我的代码:


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;
    }

This is the HAL+JSON Data: 这是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"
        ],

When I start this i just get this Error: 当我开始这个我只是得到这个错误:

07-17 15:03:41.113 25811-25835/? 07-17 15:03:41.113 25811-25835 /? E/JSONException: Error: org.json.JSONException: Value {"rh:doc":[{"_id":{"$oid":"578b712226f1a7f670df2ae9"},"_etag":{"$oid":"578b719e0106fa1728fdc730"},"produktname":"Badehaube","kategorie":"Sportartikel","preis":25,"beschreibung":"Badehaube","..... E / JSONException:错误:org.json.JSONException:Value {“rh:doc”:[{“_ id”:{“$ oid”:“578b712226f1a7f670df2ae9”},“_ etag”:{“$ oid”:“578b719e0106fa1728fdc730” }, “produktname”: “Badehaube”, “kategorie”: “Sportartikel”, “PREIS”:25, “beschreibung”: “Badehaube”,“.....

You know how to parse this? 你知道如何解析这个吗?

Here: 这里:

JSONArray jsonArray = jsonObject.getJSONArray("_embedded");

_embedded is JSONObject which contain rh:doc JSONArray .So need to get rh:doc JSONArray from _embedded : _embeddedJSONObject含有rh:doc JSONArray 。所以需要得到rh:doc JSONArray_embedded

JSONObject _embeddedObject = jsonObject.getJSONObject("_embedded");
//rh:doc JSONArray
JSONObject rndocJSONArray = _embeddedObject.getJSONArray("rh:doc");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM