diff --git a/README.md b/README.md index 70828be..ce4685f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Android JSON viewer, to convert JSON Strings to a Friendly Readable Format, it s ## Dependencies ``` -compile 'com.yuyh.json:jsonviewer:1.0.2' +compile 'com.yuyh.json:jsonviewer:1.0.3' ``` ## Usage diff --git a/app/src/main/assets/demo.json b/app/src/main/assets/demo.json index f423a08..d9cb770 100644 --- a/app/src/main/assets/demo.json +++ b/app/src/main/assets/demo.json @@ -1,4 +1,5 @@ { + "xxx": {}, "current": { "feelsLike": { "unit": "℃", @@ -713,7 +714,6 @@ "status": 0 }, "alerts": [ - ], "yesterday": { "aqi": "53", diff --git a/jsonviewer/build.gradle b/jsonviewer/build.gradle index 36bbb77..74c57a3 100644 --- a/jsonviewer/build.gradle +++ b/jsonviewer/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 11 targetSdkVersion 26 - versionCode 1 - versionName "1.0.0" + versionCode 4 + versionName "1.0.3" } buildTypes { diff --git a/jsonviewer/publish.gradle b/jsonviewer/publish.gradle index 6584a2c..b439497 100644 --- a/jsonviewer/publish.gradle +++ b/jsonviewer/publish.gradle @@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray' def siteUrl = 'https://github.com/smuyyh/JsonViewer' // Git项目主页 def gitUrl = 'https://github.com/smuyyh/JsonViewer.git' // Git仓库url group = "com.yuyh.json" // 一般为包名 -version = "1.0.2" // 版本号 +version = "1.0.3" // 版本号 install { repositories.mavenInstaller { diff --git a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/JsonRecyclerView.java b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/JsonRecyclerView.java index 5904d05..c0da025 100644 --- a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/JsonRecyclerView.java +++ b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/JsonRecyclerView.java @@ -58,27 +58,27 @@ public void bindJson(JSONObject object) { } public void setKeyColor(int color) { - BaseJsonViewerAdapter.keySpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.KEY_COLOR = color; } public void setValueTextColor(int color) { - BaseJsonViewerAdapter.textSpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.TEXT_COLOR = color; } public void setValueNumberColor(int color) { - BaseJsonViewerAdapter.numberSpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.NUMBER_COLOR = color; } public void setValueUrlColor(int color) { - BaseJsonViewerAdapter.urlSpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.URL_COLOR = color; } public void setValueNullColor(int color) { - BaseJsonViewerAdapter.nullSpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.NUMBER_COLOR = color; } public void setBracesColor(int color) { - BaseJsonViewerAdapter.bracesSpan = new ForegroundColorSpan(color); + BaseJsonViewerAdapter.BRACES_COLOR = color; } public void setTextSize(int sizeDP) { diff --git a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/BaseJsonViewerAdapter.java b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/BaseJsonViewerAdapter.java index 9ba2e28..c942273 100644 --- a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/BaseJsonViewerAdapter.java +++ b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/BaseJsonViewerAdapter.java @@ -8,10 +8,10 @@ */ public abstract class BaseJsonViewerAdapter extends RecyclerView.Adapter { - public static ForegroundColorSpan keySpan = new ForegroundColorSpan(0xFF922799); - public static ForegroundColorSpan textSpan = new ForegroundColorSpan(0xFF3AB54A); - public static ForegroundColorSpan numberSpan = new ForegroundColorSpan(0xFF25AAE2); - public static ForegroundColorSpan urlSpan = new ForegroundColorSpan(0xFF66D2D5); - public static ForegroundColorSpan nullSpan = new ForegroundColorSpan(0xFFEF5935); - public static ForegroundColorSpan bracesSpan = new ForegroundColorSpan(0xFF4A555F); + public static int KEY_COLOR = 0xFF922799; + public static int TEXT_COLOR = 0xFF3AB54A; + public static int NUMBER_COLOR = 0xFF25AAE2; + public static int URL_COLOR = 0xFF66D2D5; + public static int NULL_COLOR = 0xFFEF5935; + public static int BRACES_COLOR = 0xFF4A555F; } diff --git a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/JsonViewerAdapter.java b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/JsonViewerAdapter.java index 3ca3b94..26fc23b 100644 --- a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/JsonViewerAdapter.java +++ b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/adapter/JsonViewerAdapter.java @@ -3,6 +3,7 @@ import android.support.v7.widget.RecyclerView; import android.text.SpannableStringBuilder; import android.text.Spanned; +import android.text.style.ForegroundColorSpan; import android.view.View; import android.view.ViewGroup; @@ -75,6 +76,8 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) { itemView.hideIcon(); itemView.showRight("}"); return; + } else if (mJSONObject.names() == null) { + return; } String key = mJSONObject.names().optString(position - 1); // 遍历key @@ -92,8 +95,7 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) { itemView.hideIcon(); itemView.showRight("["); return; - } - if (position == getItemCount() - 1) { + } else if (position == getItemCount() - 1) { itemView.hideLeft(); itemView.hideIcon(); itemView.showRight("]"); @@ -112,7 +114,11 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) { @Override public int getItemCount() { if (mJSONObject != null) { - return mJSONObject.names().length() + 2; + if (mJSONObject.names() != null) { + return mJSONObject.names().length() + 2; + } else { + return 2; + } } if (mJSONArray != null) { return mJSONArray.length() + 2; @@ -132,8 +138,8 @@ public int getItemCount() { private void handleJsonObject(String key, Object value, JsonItemView itemView, boolean appendComma, int hierarchy) { SpannableStringBuilder keyBuilder = new SpannableStringBuilder(Utils.getHierarchyStr(hierarchy)); keyBuilder.append("\"").append(key).append("\"").append(":"); - keyBuilder.setSpan(keySpan, 0, keyBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - keyBuilder.setSpan(bracesSpan, keyBuilder.length() - 1, keyBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + keyBuilder.setSpan(new ForegroundColorSpan(KEY_COLOR), 0, keyBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + keyBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), keyBuilder.length() - 1, keyBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); itemView.showLeft(keyBuilder); @@ -164,32 +170,34 @@ private void handleValue(Object value, JsonItemView itemView, boolean appendComm SpannableStringBuilder valueBuilder = new SpannableStringBuilder(); if (value instanceof Number) { valueBuilder.append(value.toString()); - valueBuilder.setSpan(numberSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(NUMBER_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else if (value instanceof JSONObject) { itemView.showIcon(true); valueBuilder.append("Object{...}"); + valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); itemView.setIconClickListener(new JsonItemClickListener(value, itemView, appendComma, hierarchy + 1)); } else if (value instanceof JSONArray) { itemView.showIcon(true); valueBuilder.append("Array[").append(String.valueOf(((JSONArray) value).length())).append("]"); - valueBuilder.setSpan(bracesSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - valueBuilder.setSpan(numberSpan, 6, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - valueBuilder.setSpan(bracesSpan, valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + int len = valueBuilder.length(); + valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(NUMBER_COLOR), 6, len - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), len - 1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); itemView.setIconClickListener(new JsonItemClickListener(value, itemView, appendComma, hierarchy + 1)); } else if (value instanceof String) { itemView.hideIcon(); valueBuilder.append("\"").append(value.toString()).append("\""); if (Utils.isUrl(value.toString())) { - valueBuilder.setSpan(textSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - valueBuilder.setSpan(urlSpan, 1, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - valueBuilder.setSpan(textSpan, valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(URL_COLOR), 1, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else { - valueBuilder.setSpan(textSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } else if (valueBuilder.length() == 0 || value == null) { itemView.hideIcon(); valueBuilder.append("null"); - valueBuilder.setSpan(nullSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + valueBuilder.setSpan(new ForegroundColorSpan(NULL_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } if (appendComma) { valueBuilder.append(","); @@ -224,7 +232,7 @@ public void onClick(View view) { itemView.setTag(itemView.getRightText()); itemView.showRight(isJsonArray ? "[" : "{"); JSONArray array = isJsonArray ? (JSONArray) value : ((JSONObject) value).names(); - for (int i = 0; i < array.length(); i++) { + for (int i = 0; array != null && i < array.length(); i++) { JsonItemView childItemView = new JsonItemView(itemView.getContext()); Object childValue = array.opt(i); if (isJsonArray) { diff --git a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/view/JsonItemView.java b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/view/JsonItemView.java index 1c2adc3..5be0e9f 100644 --- a/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/view/JsonItemView.java +++ b/jsonviewer/src/main/java/com/yuyh/jsonviewer/library/view/JsonItemView.java @@ -10,6 +10,7 @@ import android.widget.TextView; import com.yuyh.jsonviewer.library.R; +import com.yuyh.jsonviewer.library.adapter.BaseJsonViewerAdapter; /** * Created by yuyuhang on 2017/11/29. @@ -55,6 +56,7 @@ private void initView() { mTvLeft.setTextSize(TEXT_SIZE_DP); mTvRight.setTextSize(TEXT_SIZE_DP); + mTvRight.setTextColor(BaseJsonViewerAdapter.BRACES_COLOR); mTvIcon.setTextSize(TEXT_SIZE_DP - 4); }