Exemplo n.º 1
0
 public List<JsonStream> readStreamRepeated(
     JSONObject jsonObject, String fieldLabel, int fieldNumber)
     throws InvalidProtocolBufferException {
   List<JsonStream> fieldStreamRepeated = null;
   if (jsonObject != null) {
     JSONValue fieldValue = jsonObject.get(fieldLabel);
     if (fieldValue != null) {
       JSONArray fieldJSONArray = jsonValueToArray(fieldValue);
       if (fieldJSONArray != null) {
         fieldStreamRepeated = new ArrayList<JsonStream>();
         for (int i = 0; i < fieldJSONArray.size(); ++i) {
           JsonStream fieldStream = null;
           JSONObject fieldJSONObject = jsonValueToObject(fieldJSONArray.get(i));
           if (fieldJSONObject != null) {
             fieldStream = this.newStream(fieldJSONObject);
           }
           if (fieldStream != null) {
             fieldStreamRepeated.add(fieldStream);
           } else {
             throw InvalidProtocolBufferException.failedToReadObjectRepeated(fieldLabel);
           }
         }
       } else {
         throw InvalidProtocolBufferException.failedToReadObjectRepeated(fieldLabel);
       }
     }
   }
   return fieldStreamRepeated;
 }
Exemplo n.º 2
0
 public void testPut_index_1_Enum_2() {
   JSONArray jsonArray = new JSONArray();
   jsonArray.element(1, (JsonEnum) null);
   assertEquals(2, jsonArray.size());
   assertEquals(JSONNull.getInstance(), jsonArray.get(0));
   assertEquals(JSONNull.getInstance(), jsonArray.get(1));
 }
Exemplo n.º 3
0
 protected List<String> readStringRepeated(
     JSONObject jsonObject, String fieldLabel, int fieldNumber)
     throws InvalidProtocolBufferException {
   List<String> fieldStringRepeated = null;
   if (jsonObject != null && fieldLabel != null) {
     JSONValue fieldValue = jsonObject.get(fieldLabel);
     if (fieldValue != null) {
       JSONArray fieldJSONArray = jsonValueToArray(fieldValue);
       if (fieldJSONArray != null) {
         fieldStringRepeated = new ArrayList<String>();
         for (int i = 0; i < fieldJSONArray.size(); ++i) {
           String fieldString = jsonValueToString(fieldJSONArray.get(i));
           if (fieldString != null) {
             fieldStringRepeated.add(fieldString);
           } else {
             throw InvalidProtocolBufferException.failedToReadStringRepeated(fieldLabel);
           }
         }
       } else {
         throw InvalidProtocolBufferException.failedToReadStringRepeated(fieldLabel);
       }
     }
   }
   return fieldStringRepeated;
 }
Exemplo n.º 4
0
 public void testPut_index_1_Enum() {
   JSONArray jsonArray = new JSONArray();
   jsonArray.element(1, JsonEnum.ARRAY);
   assertEquals(2, jsonArray.size());
   assertEquals(JSONNull.getInstance(), jsonArray.get(0));
   assertEquals("ARRAY", jsonArray.get(1));
 }
Exemplo n.º 5
0
 public static java.util.List<Purchase> arrayToList(JSONArray array) {
   java.util.List<Purchase> list = new java.util.ArrayList<Purchase>();
   for (int i = 0, len = array.size(); i < len; i++) {
     JSONObject jsonObject = array.getJSONObject(i);
     Purchase model = jsonToObject(jsonObject);
     list.add(model);
   }
   return list;
 }
Exemplo n.º 6
0
 public List<String[]> readAll() throws IOException {
   List<String[]> all;
   try {
     Object parse = parser.parse(br);
     JSONObject obj2 = (JSONObject) parse;
     JSONArray records = (JSONArray) obj2.get("records");
     all = new ArrayList(records.size());
     for (Object record : records) {
       JSONArray values = (JSONArray) record;
       List<String> v = new ArrayList<String>(values.size());
       for (Object value : values) {
         v.add(String.valueOf(value));
       }
       all.add(v.toArray(new String[v.size()]));
     }
   } catch (ParseException e1) {
     throw new RuntimeException(e1);
   } catch (IOException e1) {
     throw e1;
   }
   return all;
 }
Exemplo n.º 7
0
 public static String getZipcode(JSONObject data) {
   try {
     JSONArray results = data.getJSONArray("results");
     JSONObject address = results.getJSONObject(0);
     JSONArray address_components = address.getJSONArray("address_components");
     for (int i = 0; i < address_components.size(); i++) {
       JSONObject postal_code = address_components.getJSONObject(i);
       JSONArray types = postal_code.getJSONArray("types");
       if (types.getString(0).equals("postal_code")) {
         return (String) postal_code.get("long_name");
       }
     }
     return "0";
   } catch (NullPointerException npe) {
     npe.printStackTrace();
   }
   return null;
 }
Exemplo n.º 8
0
  public void Json2java(String str) {
    try {

      JSONObject jar = JSONObject.fromObject(str);
      JSONArray array = jar.getJSONArray("books");
      json_length = array.size();
      System.out.println(json_length);

      bklist = new ArrayList<DouBan_Book>();
      for (int i = 0; i < json_length; i++) {
        System.out.print(i + "¡¢");
        System.out.println(array.get(i));
        jsonobj = array.getJSONObject(i);
        DouBan_Book bk = new DouBan_Book();
        bk.setId(jsonobj.getString("id"));
        bk.setTitle(jsonobj.getString("title"));
        bk.setIsbn10(jsonobj.getString("isbn10"));
        // bk.setIsbn13(jsonobj.getString("isbn13"));
        bk.setImagePath(jsonobj.getString("image"));
        bk.setUrl(jsonobj.getString("url"));
        bk.setAuthor(jsonobj.getString("author"));
        bk.setBinding(jsonobj.getString("binding"));
        bk.setPages(jsonobj.getString("pages"));
        bk.setPrice(jsonobj.getString("price"));
        bk.setPubdate(jsonobj.getString("pubdate"));
        bk.setPublisher(jsonobj.getString("publisher"));
        bk.setSummary(jsonobj.getString("summary"));
        // bklist=new  ArrayList<DouBan_Book>();
        bklist.add(bk);
      }

    } catch (Exception r) {
      r.printStackTrace();
    }
    System.out.println(bklist.size());
  }
Exemplo n.º 9
0
 // This method assumes that the JSON value being passed is a JSON
 // non-primitive:
 // either an object or an array.
 // This helps to make this implementation as efficient as possible.
 protected String jsonNonPrimitiveToPrettyString(JSONValue jsonValue, int indentLevel) {
   StringBuffer buffer = new StringBuffer();
   // If the value in question is an object
   JSONObject jsonValueObject = jsonValue.isObject();
   if (jsonValueObject != null) {
     boolean firstKey = true;
     for (String key : jsonValueObject.keySet()) {
       if (firstKey) {
         firstKey = false;
       } else {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       buffer.append(key).append(" : ");
       JSONValue jsonObjectValue = jsonValueObject.get(key);
       if (jsonObjectValue != null) {
         if (jsonObjectValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("}");
         } else if (jsonObjectValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonObjectValue.toString());
         }
       }
     }
   } else if (jsonValue.isArray() != null) {
     // If the value in question is an array
     JSONArray jsonValueArray = jsonValue.isArray();
     for (int i = 0; i < jsonValueArray.size(); ++i) {
       if (0 < i) {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       JSONValue jsonArrayValue = jsonValueArray.get(i);
       if (jsonArrayValue != null) {
         if (jsonArrayValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("}");
         } else if (jsonArrayValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonArrayValue.toString());
         }
       }
     }
   }
   return buffer.toString();
 }
Exemplo n.º 10
0
 public void testPut_Enum() {
   JSONArray jsonArray = new JSONArray();
   jsonArray.element(JsonEnum.ARRAY);
   assertEquals(1, jsonArray.size());
   assertEquals("ARRAY", jsonArray.get(0));
 }