我无法从JSONObject中检索Id并将其传递给String以记录该玩家特定谜题的ID但在线上字符串idString = obj.getString(" Id" );
"我得到了org.json.JSONException:Id没有值"
我通过调用此类中的服务器来获取此信息,该服务器检查播放器的用户名和密码。
import android.os.AsyncTask;
import android.text.format.Time;
import android.util.Log;
public class GetTodaysPuzzle implements OnRetrieveHttpData {
public String GetTodaysPuzzle(String mUserName, String mPassword)
{
RetrieveHTTPData GetTodayspuzzle = new RetrieveHTTPData(this);
return GetTodayspuzzle.GetResponseData("urlString" + mUserName + "&password=" + mPassword);
}
@Override
public void onRetrieveTaskCompleted(String httpData) {
Log.i("Server Response", httpData);
}
class GetTodaysPuzzleTask extends AsyncTask<String, String, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
// Get response from server
String json = GetTodaysPuzzle(params[0], params[1]);
// Check if a puzzle was returned
if (json.contains("The request is invalid"))
{
return false;
}
try
{
// Calculate today's date in order to set wordsearch date
Time today = new Time();
today.setToNow();
String formattedDate = Dates.ConvertUStoUK(today.year + "-"
+ (today.month + 1) + "-" + today.monthDay);
// Create wordsearch
WordSearch wordSearch = WordSearch.CreateWordSearch(json,
formattedDate);
if (wordSearch == null)
{
return false;
}
// Save wordsearch to collection
WordSearchDatabase.Add(wordSearch);
// WordSearchDatabase.Save();
return true;
}
catch (Exception e)
{
Log.e("GetTodaysPuzzleTask", e.getMessage());
return false;
}
}
}
}
然后将所有信息存储到Wordsearch类中的JSONArrays中。
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class WordSearch {
public String Id;
public String Date;
public List<Word> Words;
public Letter[] Letters;
public final int NUM_OF_ROWS;
public final int NUM_OF_COLS;
public int Score = -1;
private boolean containsSolution = false;
public boolean canBeSubmitted = true;
private boolean complete = false;
private boolean submitted = false;
public static WordSearch CreateWordSearch(String json, String date)
{
List<Word> listOfWords = new ArrayList<Word>();
List<String> listOfRows = new ArrayList<String>();
try
{
// Create a JSON Object from JSON string
JSONObject obj = new JSONObject(json);
// Get Id
String idString = obj.getString("Id");
// GetWords & Puzzle as JSON Arrays
JSONArray wordsArray = obj.getJSONArray("Words");
JSONArray puzzleArray = obj.getJSONArray("Grid");
// Parse Words array
for (int i = 0; i < wordsArray.length(); i++)
{
String name = wordsArray.getString(i);
Word word = new Word(name);
listOfWords.add(word);
}
// Parse Puzzle
for (int k = 0; k < puzzleArray.length(); k++)
{
String puzzle = puzzleArray.getString(k);
listOfRows.add(puzzle);
}
// Create WordSearch
return new WordSearch(idString, date, listOfWords, listOfRows,
false);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
这是我第一次在Android中使用API服务器和JSONObject,所以任何帮助都会非常感激。谢谢。
答案 0 :(得分:2)
您收到此错误是因为您的回复JSON没有&#34; Id&#34;作为一个关键,它只有&#34;益智&#34;如你所见:
{
"Puzzle": {
"Id": "8fb25209-863a-410b-a440-b5b57a903ee1",
"Words": ["CHATEAUX", ...],
"Grid": ["TLIOFSHTRC", ...]
}
}
你需要先得到&#34;益智&#34;首先是对象然后你可以获得&#34; Id&#34;从那个对象。
JSONObject obj = new JSONObject(json);
JSONObject puzzle = obj.getJSONObject("Puzzle");
String idString = puzzle.getString("Id");
....
答案 1 :(得分:1)
我在拼图元素中,所以它应该是
JSONObject obj = new JSONObject(json);
//get puzzle object
JSONObject puzzle = obj.getJSONObject("Puzzle");
// Get Id
String idString = puzzle.getString("Id");
// GetWords & Puzzle as JSON Arrays
JSONArray wordsArray = puzzle.getJSONArray("Words");
JSONArray puzzleArray = puzzle.getJSONArray("Grid");