我想使用Json閱讀器來解析通過.json文件插入記錄到我的數據庫,但我有通過文件讀取問題。android JSON reader - IllegalStateException;預期的名稱,但字符串
我故意用try/catch子句打開了解析/閱讀器的方法,所以我可以看到錯誤來自哪裏。
這裏是JSON解析器/ Reader類:
package cybertech.productions.servicehelpers;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.content.Context;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
public class JsonParser {
public void readCardJsonStream1(String cardSet, CarddbAdapter yugiohDB,
Context context) throws IOException {
URL test = new URL(cardSet);
InputStream in = new BufferedInputStream(test.openStream());
JsonReader reader = new JsonReader(new InputStreamReader(in,
"ISO-8859-1"));
String s, s1, s2, levelstr, rankstr, atkstr, defstr, setnumstr, cardpasstr;
int numTotalElements = 0;
int elementsParsed = 0;
reader.beginObject();
s = reader.nextName();
reader.beginObject();
while (reader.hasNext()) {
s = reader.nextName();
if (s.equalsIgnoreCase("p")) { // cards
reader.beginObject();
while (reader.hasNext()) {
s1 = reader.nextName();
if (s1.equalsIgnoreCase("o")) { // card
YugiohCard c;
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
c = new YugiohCard();
while (reader.hasNext()) {
s2 = reader.nextName();
if (s2.equalsIgnoreCase("a")) { // name
c.name = reader.nextString();
} else if (s2.equalsIgnoreCase("b")) { // type
c.type = reader.nextString();
} else if (s2.equalsIgnoreCase("c")) { // attribute_type
c.attributeType = reader.nextString();
} else if (s2.equalsIgnoreCase("d")) { // summon_requirements
c.summonRequirements = reader.nextString();
} else if (s2.equalsIgnoreCase("e")) { // description
c.description = reader.nextString();
} else if (s2.equalsIgnoreCase("f")) { // spell
// speed
c.spellSpeed = reader.nextString();
} else if (s2.equalsIgnoreCase("g")) { // level_stars
levelstr = reader.nextString();
c.levelStars = Integer.parseInt(levelstr);
} else if (s2.equalsIgnoreCase("h")) { // rank_stars
rankstr = reader.nextString();
c.rankStars = Integer.parseInt(rankstr);
} else if (s2.equalsIgnoreCase("i")) { // atk_points
atkstr = reader.nextString();
c.atk_stat = Integer.parseInt(atkstr);
} else if (s2.equalsIgnoreCase("j")) { // def_points
defstr = reader.nextString();
c.def_stat = Integer.parseInt(defstr);
} else if (s2.equalsIgnoreCase("n")) { // set
// initials
c.setinit = reader.nextString();
} else if (s2.equalsIgnoreCase("k")) { // set_number
setnumstr = reader.nextString();
c.setNumber = Integer.parseInt(setnumstr);
} else if (s2.equalsIgnoreCase("l")) { // card_number_pass
cardpasstr = reader.nextString();
c.cardnumberPass = Integer
.parseInt(cardpasstr);
}
}
yugiohDB.createDBCard(c);
reader.endObject();
}
reader.endArray();
}
}
reader.endObject();
}
if (s.equalsIgnoreCase("w")) { // num_cards
numTotalElements = reader.nextInt();
}
}
reader.endObject();
reader.close();
}
}
這是我的JSON文件:
{
"t": {
"p": {
"o": [
{
"a": "Elemental HERO Absolute Zero",
"b": "Monster/Fusion/Effect",
"c": "WATER",
"d": "1 'HERO' monster + 1 WATER monster",
"e": "Blows up your field, sucks for you",
"f": "NULL",
"g": "8",
"h": "0",
"i": "2500",
"j": "2000",
"n": "YG04",
"k": "001",
"l": "40854197"
},
{
"a": "Elemental HERO Air Neos",
"b": "Monster/Fusion/Effect",
"c": "WIND",
"d": "Hummingbird + Neos",
"e": "Returns to extra at end phase",
"f": "NULL",
"g": "7",
"h": "0",
"i": "2500",
"j": "2000",
"n": "STON",
"k": "034",
"l": "11502550"
},
{
"a": "Cyber Jar",
"b": "Monster/Effect",
"c": "DARK",
"d": "NULL",
"e": "destroys all monsters on the field",
"f": "NULL",
"g": "3",
"h": "0",
"i": "900",
"j": "900",
"n": "MRL",
"k": "077",
"l": "34124316"
},
{
"a": "Wind-Up Carrier Zenmaity",
"b": "MACHINE/Xyz/Effect",
"c": "WATER",
"d": "2 Level 3 monsters",
"e": "Special summon a wind-up from your hand or deck.",
"f": "NULL",
"g": "0",
"h": "3",
"i": "1500",
"j": "1500",
"n": "ORCS",
"k": "044",
"l": "81122844"
},
{
"a": "BRAIN CONTROL",
"b": "Spell",
"c": "Spell",
"d": "NULL",
"e": "Pay 800 Life Points to target 1 face-up monster your opponent controls; take control of that target until the End Phase.",
"f": "Normal",
"g": "0",
"h": "0",
"i": "0",
"j": "0",
"n": "LCYW",
"k": "074",
"l": "87910978"
},
{
"a": "Ring of Destruction",
"b": "Trap",
"c": "Trap",
"d": "NULL",
"e": "Target 1 face-up monster on the field; destroy that target, and if you do, inflict damage to both players equal to that target's ATK.",
"f": "Normal",
"g": "0",
"h": "0",
"i": "0",
"j": "0",
"n": "YG04",
"k": "001",
"l": "40854197"
}
]
},
"v": "2/3/2013"
}
}
這裏是我創建告訴在JSON文件位於我的網站的方法:
public void CreateYUGIsets() throws MalformedURLException, IOException{
yugiohDB.dropDB();
JsonParser parser = new JsonParser();
String cardSetList = "https://sites.google.com/site/yugiohlibrary/patches/test.json";
parser.readCardJsonStream1(cardSetList, yugiohDB, this);
}
這裏是logcat的錯誤我得到過:
05-15 22:48:49.362: E/AndroidRuntime(476): FATAL EXCEPTION: main
05-15 22:48:49.362: E/AndroidRuntime(476): java.lang.RuntimeException: Unable to start activity ComponentInfo{cybertech.productions.yugiohlibrary/cybertech.productions.yugiohlibrary.LoadingScreen}: java.lang.IllegalStateException: Expected a name but was STRING
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:123)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-15 22:48:49.362: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 22:48:49.362: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 22:48:49.362: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 22:48:49.362: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 22:48:49.362: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(Native Method)
05-15 22:48:49.362: E/AndroidRuntime(476): Caused by: java.lang.IllegalStateException: Expected a name but was STRING
05-15 22:48:49.362: E/AndroidRuntime(476): at com.google.gson.stream.JsonReader.nextName(JsonReader.java:444)
05-15 22:48:49.362: E/AndroidRuntime(476): at cybertech.productions.servicehelpers.JsonParser.readCardJsonStream1(JsonParser.java:36)
05-15 22:48:49.362: E/AndroidRuntime(476): at cybertech.productions.yugiohlibrary.LoadingScreen.CreateYUGIsets(LoadingScreen.java:107)
05-15 22:48:49.362: E/AndroidRuntime(476): at cybertech.productions.yugiohlibrary.LoadingScreen.onCreate(LoadingScreen.java:41)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 22:48:49.362: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-15 22:48:49.362: E/AndroidRuntime(476): ... 11 more
對於在logcat的這個特定行: cybertech.productions.servicehelpers.JsonParser.readCardJsonStream1(JsonParser.java:36) 05-15 22:48:49.362:E/AndroidRuntime(476)
JsonParser類中的第36行是: s = reader.nextName();
當第一個while()循環開始時,該行就是正確的。
任何幫助,將不勝感激。
非常感謝這個信息和例子,一定會嘗試一下。 –
@Perception如何顯示列表卡片進入列表視圖,之前我使用手動邏輯來解析JSON與GSON lib,實現您的代碼後我能夠通過列表視圖不加載數據的SOP檢索數據!可以解釋如何填充名單列表卡列表視圖(Yugioh Serializable!) –
@Perception我實現了你的解決方案到這個手動解析,但列表視圖停止填充!我需要反序列化名單卡? http://stackoverflow.com/questions/18140830/gson-throwing-expected-expected-a-name-but-was-number-at-line-1-column-8/18141038?noredirect=1#18141038 –