org.json.JSONException类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(266)

本文整理了Java中org.json.JSONException类的一些代码示例,展示了JSONException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONException类的具体详情如下:
包路径:org.json.JSONException
类名称:JSONException

JSONException介绍

[英]The JSONException is thrown by the JSON.org classes when things are amiss.
[中]JSONException由JSON抛出。在出现问题时组织类。

代码示例

代码示例来源:origin: stackoverflow.com

try {
     JSONObject parent = new JSONObject();
     JSONObject jsonObject = new JSONObject();
     JSONArray jsonArray = new JSONArray();
     jsonArray.put("lv1");
     jsonArray.put("lv2");
     jsonObject.put("mk1", "mv1");
     jsonObject.put("mk2", jsonArray);
     parent.put("k2", jsonObject);
     Log.d("output", parent.toString(2));
   } catch (JSONException e) {
     e.printStackTrace();
   }

代码示例来源:origin: apache/geode

/**
 *
 * @param source A string beginning with { (left brace) and ending with } (right brace).
 * @throws GfJsonException - If there is a syntax error in the source string or a duplicated key.
 */
public GfJsonObject(String source) throws GfJsonException {
 try {
  this.jsonObject = new JSONObject(source);
 } catch (JSONException e) {
  throw new GfJsonException(e.getMessage());
 }
}

代码示例来源:origin: cSploit/android

public synchronized String getReleaseBody(String tag_name) throws JSONException, IOException {
 JSONObject release;
 String current;
 if(mReleases==null)
  fetchReleases();
 for(int i=0;i<mReleases.length();i++) {
  release = mReleases.getJSONObject(i);
  current = release.getString("tag_name");
  if(current.equals(tag_name) || current.equals("v" + tag_name))
   return release.getString("body");
 }
 throw new JSONException(String.format("release '%s' not found", tag_name));
}

代码示例来源:origin: stackoverflow.com

JSONObject jsonObj = null;
try {
  jsonObj = XML.toJSONObject(sampleXml);
} catch (JSONException e) {
  Log.e("JSON exception", e.getMessage());
  e.printStackTrace();
} 

Log.d("XML", sampleXml);

Log.d("JSON", jsonObj.toString());

代码示例来源:origin: zwwill/yanxuan-weex-demo

finish();
} else {
 JSONObject data = new JSONObject();
 try {
  data.put("WeexBundle", Uri.parse(code).toString());
  Intent intent = new Intent(WXPageActivity.this, WXPageActivity.class);
  intent.setData(Uri.parse(data.toString()));
  startActivity(intent);
 } catch (JSONException e) {
  e.printStackTrace();

代码示例来源:origin: lzyzsd/JsBridge

public static List<Message> toArrayList(String jsonStr){
    List<Message> list = new ArrayList<Message>();
    try {
      JSONArray jsonArray = new JSONArray(jsonStr);
      for(int i = 0; i < jsonArray.length(); i++){
        Message m = new Message();
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR):null);
        m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR):null);
        m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR):null);
        m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR):null);
        m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR):null);
        list.add(m);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return list;
  }
}

代码示例来源:origin: ankidroid/Anki-Android

return true;
case R.id.action_add_card_from_card_browser: {
  Intent intent = new Intent(CardBrowser.this, NoteEditor.class);
  intent.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_CARDBROWSER_ADD);
  startActivityForResultWithAnimation(intent, ADD_NOTE, ActivityTransitionAnimation.LEFT);
  return true;
  JSONObject savedFiltersObj = getCol().getConf().optJSONObject("savedFilters");
  HashMap<String, String> savedFilters = new HashMap<>();
  if (savedFiltersObj != null) {
    Iterator<String> it = savedFiltersObj.keys();
    while (it.hasNext()) {
      String searchName = it.next();
      savedFilters.put(searchName, savedFiltersObj.optString(searchName));
      arrayAdapter.add(deck.getString("name"));
    } catch (JSONException e) {
      e.printStackTrace();
  Intent previewer = new Intent(CardBrowser.this, Previewer.class);
  previewer.putExtra("index", 0);
  if (mInMultiSelectMode) {

代码示例来源:origin: stackoverflow.com

JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("formules");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
for (int i = 0; i < m_jArry.length(); i++) {
  JSONObject jo_inside = m_jArry.getJSONObject(i);
  Log.d("Details-->", jo_inside.getString("formule"));
  String formula_value = jo_inside.getString("formule");
  String url_value = jo_inside.getString("url");
e.printStackTrace();

代码示例来源:origin: yuliskov/SmartYouTubeTV

/**
 * Send off an intent to start the download of the app.
 */
public void startUpgrade() {
  try {
    final Uri downloadUri = Uri.parse(pkgInfo.getString("downloadUrl"));
    mContext.startActivity(new Intent(Intent.ACTION_VIEW, downloadUri));
  } catch (final JSONException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: jeasonlzy/NineGridView

@Override
  public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
    try {
      emptyView.setVisibility(View.GONE);
      JSONArray object = new JSONObject(s).getJSONObject("showapi_res_body").getJSONArray("channelList");
      Type channelItemType = new TypeToken<List<NewsChannel>>() {}.getType();
      List<NewsChannel> channelItems = new Gson().fromJson(object.toString(), channelItemType);
      viewPager.setAdapter(new ChannelAdapter(getSupportFragmentManager(), channelItems));
      tab.setViewPager(viewPager);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
});

代码示例来源:origin: stackoverflow.com

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {
      // JSON here
    } catch (JSONException e2) {
      // TODO Auto-generated catch block
      e2.printStackTrace();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    setContentView(R.layout.activity_main);
    Intent intent=new Intent(this,HomeActivity.class);
    startActivity(intent);
  }
}

代码示例来源:origin: stackoverflow.com

JSONObject featureCollection = new JSONObject();
 try {
   featureCollection.put("type", "featureCollection");
   JSONArray featureList = new JSONArray();
   // iterate through your list
   for (ListElement obj : list) {
     // {"geometry": {"type": "Point", "coordinates": [-94.149, 36.33]}
     JSONObject point = new JSONObject();
     point.put("type", "Point");
     // construct a JSONArray from a string; can also use an array or list
     JSONArray coord = new JSONArray("["+obj.getLon()+","+obj.getLat()+"]");
     point.put("coordinates", coord);
     JSONObject feature = new JSONObject();
     feature.put("geometry", point);
     featureList.put(feature);
     featureCollection.put("features", featureList);
   }
 } catch (JSONException e) {
   Log.error("can't save json object: "+e.toString());
 }
 // output the result
 System.out.println("featureCollection="+featureCollection.toString());

代码示例来源:origin: stackoverflow.com

String json = "Assuming that here is your JSON response"; 
try {
  JSONObject parentObject = new JSONObject(json);
  JSONObject userDetails = parentObject.getJSONObject("user_details"); 

  //And then read attributes like             
  String name = userDetails.getString("user_name"); 
  String phone = userDetails.getString("user_phone");
  String id = userDetails.getString("re‌​f_id");

} catch (JSONException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

代码示例来源:origin: zwwill/yanxuan-weex-demo

Uri uri = intent.getData();
String from = intent.getStringExtra("from");
mFromSplash = "splash".equals(from);
  JSONObject initData = new JSONObject(uri.toString());
  String bundleUrl = initData.optString("WeexBundle", null);
  if (bundleUrl != null) {
   mUri = Uri.parse(bundleUrl);
  String ws = initData.optString("Ws", null);
  if (!TextUtils.isEmpty(ws)) {
   mHotReloadManager = new HotReloadManager(ws, new HotReloadManager.ActionListener() {
  e.printStackTrace();

代码示例来源:origin: lzyzsd/JsBridge

public static Message toObject(String jsonStr) {
  Message m =  new Message();
  try {
    JSONObject jsonObject = new JSONObject(jsonStr);
    m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR):null);
    m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR):null);
    m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR):null);
    m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR):null);
    m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR):null);
    return m;
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return m;
}

代码示例来源:origin: lzyzsd/JsBridge

public String toJson() {
  JSONObject jsonObject= new JSONObject();
  try {
    jsonObject.put(CALLBACK_ID_STR, getCallbackId());
    jsonObject.put(DATA_STR, getData());
    jsonObject.put(HANDLER_NAME_STR, getHandlerName());
    String data = getResponseData();
    if (TextUtils.isEmpty(data)) {
     jsonObject.put(RESPONSE_DATA_STR, data);
    } else {
     jsonObject.put(RESPONSE_DATA_STR, new JSONTokener(data).nextValue());
    }
    jsonObject.put(RESPONSE_DATA_STR, getResponseData());
    jsonObject.put(RESPONSE_ID_STR, getResponseId());
    return jsonObject.toString();
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: facebook/facebook-android-sdk

protected String getClientState(String authId) {
  JSONObject param = new JSONObject();
  try {
    param.put(LoginLogger.EVENT_PARAM_AUTH_LOGGER_ID, authId);
    param.put(LoginLogger.EVENT_PARAM_METHOD, getNameForLogging());
    putChallengeParam(param);
  } catch (JSONException e) {
    Log.w("LoginMethodHandler", "Error creating client state json: " + e.getMessage());
  }
  return param.toString();
}

代码示例来源:origin: stackoverflow.com

protected void onPostExecute(Void v) {
    //parse JSON data
    try {
      JSONArray jArray = new JSONArray(result);    
      for(i=0; i < jArray.length(); i++) {

        JSONObject jObject = jArray.getJSONObject(i);

        String name = jObject.getString("name");
        String tab1_text = jObject.getString("tab1_text");
        int active = jObject.getInt("active");

      } // End Loop
      this.progressDialog.dismiss();
    } catch (JSONException e) {
      Log.e("JSONException", "Error: " + e.toString());
    } // catch (JSONException e)
  } // protected void onPostExecute(Void v)
} //class MyAsyncTask extends AsyncTask<String, String, Void>

代码示例来源:origin: stackoverflow.com

@Override
public void onReceive(Context context, Intent intent) {
  Bundle extras = intent.getExtras();
  String jsonData = extras.getString("com.parse.Data");
  JSONObject jsonObject;
  try {
    jsonObject = new JSONObject(jsonData);
    String heading = jsonObject.getString("heading");
    String dataString = jsonObject.getString("dataString");
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: stackoverflow.com

JSONObject obj = new JSONObject(json);
  trimmedString = obj.getString(key);
} catch(JSONException e){
  e.printStackTrace();
  return null;

相关文章