Code examples for Parsing JSON documents

Codota analyzed 7,668,838 open source code samples to find the most common, credible and clear examples.
You can now enable Codota on your own code to easily search and navigate your Java codebase.
      if (matcher.matches()) { 
        // Handle as array 
        String fieldName = matcher.group(1); 
        int index = Integer.parseInt(matcher.group(2)); 
        JSONArray newArrayStep; 
        if (root.has(fieldName)) { 
          newArrayStep = root.getJSONArray(fieldName); 
        } else { 
          newArrayStep = new JSONArray(); 
          root.put(fieldName, newArrayStep); 
        } 
        JSONObject newStep = new JSONObject(); 
        newArrayStep.put(index, newStep); 
        return buildHolder(newStep, steps, ++currentStep); 
      } else { 
        JSONObject newStep; 
        if (root.has(steps[currentStep])) { 
          newStep = root.getJSONObject(steps[currentStep]); 
        } else { 
          newStep = new JSONObject(); 

10
CodeRank
        public ArrayList<TrainDeparture> trains = new ArrayList<TrainDeparture>(); 
 
        public static Departures fromJson(JSONObject json) throws JSONException { 
            Departures d = new Departures(); 
 
            if (!json.isNull("serves_types")) { 
                JSONArray jsonServesTypes = json.getJSONArray("serves_types"); 
                for (int i = 0; i < jsonServesTypes.length(); i++) { 
                    d.servesTypes.add(jsonServesTypes.getString(i)); 
                } 
            } 
 
            if (!json.isNull("metros")) { 
                JSONObject jsonMetros = json.getJSONObject("metros"); 
                if (jsonMetros.has("group_of_lines")) { 
                    d.metros.add(MetroDeparture.fromJson(jsonMetros)); 
                } 
            } 
 
            if (!json.isNull("buses")) { 
                JSONArray jsonBuses = json.getJSONArray("buses"); 
Full Snippet Info
7
CodeRank
import org.json.JSONWriter; 
 
import com.google.refine.Jsonizable; 
import com.google.refine.ProjectManager; 
import com.google.refine.ProjectMetadata; 
import com.google.refine.model.Project; 
import com.google.refine.util.JSONUtilities; 
 
 
public class ImportingJob implements Jsonizable { 
    final public long id; 
    final public File dir; // Temporary directory where the data about this job is stored 
     
    private JSONObject config; 
     
    public Project project; 
    public ProjectMetadata metadata; 
     
    public long lastTouched; 
    public boolean updating; 

7
CodeRank
                    System.err.println("Empty response during initialization."); 
                } 
                return false; 
            } 
 
            JSONObject json = new JSONObject(new String(b)); 
 
            this.uuid = json.getString("uuid"); 
 
            JSONObject proto = json.getJSONObject("protocol"); 
 
            if (!isCompatible(proto)) { 
                if (debug) { 
                    System.err 
                            .println("Driver not compatible with adapter protocol: " 
                                    + proto); 
                } 
                return false; 
            } 
 

7
CodeRank
	private static final long serialVersionUID = -6166266603350967003L; 
 
	public DirectMessageJSON() { 
	} 
	public DirectMessageJSON(JSONObject json) throws Exception { 
		_createdAt = Time.getTwitterDate(json.getString("created_at")); 
		_senderScreenName = json.getString("sender_screen_name"); 
		_sender = new UserJSON(json.getJSONObject("sender")); 
		_text = Utils.unescape(json.getString("text")); 
		_recipientScreenName = json.getString("recipient_screen_name"); 
		_id = json.getLong("id"); 
		_recipient = new UserJSON(json.getJSONObject("recipient")); 
		_recipientId = json.getLong("recipient_id"); 
		_senderId = json.getLong("sender_id"); 
	} 
	public DirectMessageJSON(JSONObject json, boolean outgoing) throws Exception { 
		_createdAt = Time.getTwitterDate(json.getString("created_at")); 
		_senderScreenName = json.getString("sender_screen_name"); 
		_sender = new UserJSON(json.getJSONObject("sender")); 
		_text = Utils.unescape(json.getString("text")); 
Full Snippet Info
7
CodeRank
public class TrendLocationJSON implements TrendLocation, Serializable { 
 
	private static final long serialVersionUID = 3666580020698044751L; 
 
	public TrendLocationJSON(JSONObject json) throws Exception { 
        _woeid = json.optInt("woeid"); 
        _name = json.optString("name"); 
        _countryCode = json.optString("countryCode"); 
        _parentId = json.optInt("parentid"); 
        _url = json.optString("url"); 
        _country = json.optString("country"); 
        if(!json.isNull("placeType")) { 
            JSONObject placeType = json.getJSONObject("placeType"); 
            _placeName = placeType.optString("name"); 
            _placeCode = placeType.optInt("code"); 
        } 
    } 
 
    private int _woeid; 
    private String _name; 
Full Snippet Info
7
CodeRank
        assertEquals(0, resp.getContentLength()); 
 
        // Get the preferences 
 
        resp = sendRequest(new GetRequest(URL), 200); 
        jsonResult = new JSONObject(resp.getContentAsString()); 
        assertNotNull(jsonResult); 
        assertTrue(jsonResult.keys().hasNext()); 
 
        checkJSONObject(jsonResult); 
        checkJSONObject(jsonResult.getJSONObject("comp1")); 
 
        // Update some of the preferences 
 
        jsonObject.put("stringValue", "updated"); 
        jsonObject.put("comp2", getPreferenceObj()); 
 
        resp = sendRequest(new PostRequest(URL, jsonObject.toString(), "application/json"), 200); 
        assertEquals(0, resp.getContentLength()); 
 

7
CodeRank
    } 
 
    private JSONObject cloneJSON( JSONObject jsonObject ) 
        throws JSONException 
    { 
        String[] names = JSONObject.getNames( jsonObject ); 
        if( names == null ) 
        { 
            names = EMPTY_NAMES; 
        } 
        return new JSONObject( jsonObject, names ); 
    } 
 
    @Override 
    public EntityReference associationValueOf( QualifiedName stateName ) 
    { 
        try 
        { 
            Object jsonValue = state.getJSONObject( JSON_KEY_ASSOCIATIONS ).opt( stateName.name() ); 
            if( jsonValue == null ) 

7
CodeRank
    this.width = width; 
 
  } 
 
  public UIARect(JSONObject o) throws JSONException { 
    JSONObject origin = o.getJSONObject("origin"); 
    x = origin.getInt("x"); 
    y = origin.getInt("y"); 
 
    JSONObject size = o.getJSONObject("size"); 
    height = size.getInt("height"); 
    width = size.getInt("width"); 
  } 
 
  public String toString() { 
    return "origin(x=" + x + ",y=" + y + "),size(height=" + height + ",width=" + width + ")"; 
  } 
 
  public int getX() { 
    return x; 
Full Snippet Info
7
CodeRank
	User sender; 
	String id; 
 
	public Comment(JSONObject obj, String accessToken) 
			throws JSONException { 
		super(obj, accessToken); 
		setCreatedTimestamp(obj.getString("created_time")); 
		setText(obj.getString("text")); 
		setId(obj.getString("id")); 
		setSender((new User(obj.getJSONObject("from"), accessToken))); 
	} 
 
	public String getCreatedTimestamp() { 
		return createdTimestamp; 
	} 
 
	protected void setCreatedTimestamp(String createdTimestamp) { 
		this.createdTimestamp = createdTimestamp; 
	} 
 
Full Snippet Info
7
CodeRank