2014-10-20 10 views
19

Sto cercando di leggere e analizzare una stringa JSON che inizia come un array (ad esempio [{test: "test"}]) e continuo a correre in errore:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 

Il errore nei miei punti di registro a questa linea:

Gson gson = new GsonBuilder().create(); 
PayoutCharges payoutList = gson.fromJson(reader, PayoutCharges.class); 

Dopo alcune risposte StackOverflow, ho creato la classe PayoutCharges come una lista di array di PayoutCharge. Come posso risolvere questo problema in modo che GSON sappia che la stringa JSON si trova all'interno di un array?

PayoutCharges.java

package com.app.driver.entity; 

import java.util.ArrayList; 

import com.google.gson.annotations.SerializedName; 

public class PayoutCharges { 
    //handle error 
    @SerializedName("error") 
    private Error mError; 

    public Error getError() { 
     return mError; 
    } 

    public void setError(Error error) { 
     mError = error; 
    } 

    //handle data 
    @SerializedName("payoutCharges") 
    private ArrayList<PayoutCharge> mPayoutCharges; 

    public ArrayList<PayoutCharge> getPayoutCharges() { 
     return mPayoutCharges; 
    } 

    public void setPayoutCharges(ArrayList<PayoutCharge> payoutCharges) { 
     mPayoutCharges = payoutCharges; 
    } 
} 

Dopo aver letto la risposta di @ Ridcully, voglio chiedere se c'è un modo per me di aggiornare PayoutCharges.java in modo che sappia che il JSON è un array. Qualcosa come @SerializedName([])?

risposta

25

Sotto codice funziona per il vostro campione valore JSON:

String val1 = "[{test: \"test\"}]"; 

final GsonBuilder gsonBuilder = new GsonBuilder(); 
final Gson gson = gsonBuilder.create(); 

TestCase[] testCase = gson.fromJson(val1, TestCase[].class); 

La classe titolare del TestCase:

private static class TestCase { 
    @SerializedName("test") 
    private String field; 
} 

L'esempio di test che hai condiviso ha una matrice che ha oggetti. Quindi devi usare un array della tua classe Pojo mentre deserializzi il valore JSON in un oggetto (array).

Se questa risposta non ti aiuta (il che significa che hai qualcosa di diverso sul tuo vero valore json), dovresti condividere meglio il vero json su cui stai lavorando.

+0

Tuncer hai salvato la mia giornata. Grazie per la soluzione – PPD

+0

Semplice ma ottimo esempio, questo ha risolto il mio problema !, Grazie – RoDo

+0

Grazie al fatto che funziona –

0

Se si definisce la matrice di PayoutCharge oggetti ancora una volta come una classe (PayoutCharges), il vostro JSON dovrebbe essere simile a questo:

{"error" : <... JSONified Error object>, 
"payoutCharges" : [{"test" : "test"}] 
} 
+0

c'è un modo per me di mantenere la JSON come è (l'applicazione iOS è anche utilizzando la risposta JSON quindi devo evitare di manipolarlo)? – Huy

-1

Fare attenzione al secondo parmetro di gson.fromJson(), ha bisogno di una classe di array. Per esempio dati []. Classe.

+0

Se corretto, questo non aggiunge nulla oltre la risposta già accettata, che è molto più prolissa. Prova a rispondere a una domanda più recente, idealmente quella che non ha ancora avuto risposta. – Benjamin

0

enter image description here sopra è quello che devo analizzare; qui è il mio soggetto:

**package com.winway.ecloud.data.entity; 

import java.util.List; 


public class WellSectionEntity { 


     private String name;  

     private String picture; 

     private String remark; 

     private List<Point> pos;  

     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
      this.name = name; 
     } 
     public String getPicture() { 
      return picture; 
     } 
     public void setPicture(String picture) { 
      this.picture = picture; 
     } 
     public String getRemark() { 
      return remark; 
     } 
     public void setRemark(String remark) { 
      this.remark = remark; 
     } 
     public List<Point> getPos() { 
      return pos; 
     } 
     public void setPos(List<Point> pos) { 
      this.pos = pos; 
     } 

} 
package com.winway.ecloud.data.entity; 


public class Point { 
    public float x;// 
    public float y;// 
    public float circleR = 50;// 
    public float r;// 
    private String lineNO;// 
    private String lineName;// 
    private String no;// 
    private int deep;// 


    public float getX() { 
     return x; 
    } 

    public void setX(float x) { 
     this.x = x; 
    } 

    public float getY() { 
     return y; 
    } 

    public void setY(float y) { 
     this.y = y; 
    } 

    public float getR() { 
     return r; 
    } 

    public float getCircleR() { 
     return circleR; 
    } 

    public void setCircleR(float circleR) { 
     this.circleR = circleR; 
    } 

    public void setR(float r) { 
     this.r = r; 
    } 

    public String getLineNO() { 
     return lineNO; 
    } 

    public void setLineNO(String lineNO) { 
     this.lineNO = lineNO; 
    } 

    public String getLineName() { 
     return lineName; 
    } 

    public void setLineName(String lineName) { 
     this.lineName = lineName; 
    } 

    public String getNo() { 
     return no; 
    } 

    public void setNo(String msg) { 
     this.no = msg; 
    } 

    public int getDeep() { 
     return deep; 
    } 

    public void setDeep(int deep) { 
     this.deep = deep; 
    } 
} 

final GsonBuilder gsonBuilder = new GsonBuilder(); 
     final Gson gson = gsonBuilder.create(); 

jsonTemp è una stringa, che includono quello che elencare a capo di questo questo arcticle solution1:

WellSectionEntity[] listSection = gson.fromJson(***jsonTemp***, WellSectionEntity[].class); 

Solution2:

List<WellSectionEntity> sectionlist = gson.fromJson(jsonTemp, new TypeToken<List<WellSectionEntity>>(){}.getType()); 

grazie DevrimTuncer.

-2

Questo perché il nome e il tipo della seconda colonna nell'oggetto sono in conflitto con i dati analizzati. Se non si dispone di uno dei dati nei dati, è possibile scriverlo nell'ultima colonna.

image description

+0

Invece di uno screenshot, è meglio fornire il tuo codice come Testo all'interno di un codice –

+0

或者 你 可以 检查 一下 正确 正确, 比如 "[[" 的 错误 符号 Oppure puoi verificare se i simboli corretti, come "libero" "simbolo di errore – keinta

Problemi correlati