如何添加com.google.gson.JsonArray的特定索引?

时间:2017-05-29 08:25:59

标签: java arrays json gson

#include <SoftwareSerial.h> SoftwareSerial GSM(9, 10); // RX, TX int sensor=5; enum _parseState { PS_DETECT_MSG_TYPE, PS_IGNORING_COMMAND_ECHO, PS_HTTPACTION_TYPE, PS_HTTPACTION_RESULT, PS_HTTPACTION_LENGTH, PS_HTTPREAD_LENGTH, PS_HTTPREAD_CONTENT }; byte parseState = PS_DETECT_MSG_TYPE; char buffer[80]; byte pos = 0; int contentLength = 0; void resetBuffer() { memset(buffer, 0, sizeof(buffer)); pos = 0; } void sendGSM(const char* msg, int waitMs = 500) { GSM.println(msg); delay(waitMs); while(GSM.available()) { parseATText(GSM.read()); } } void setup() { GSM.begin(9600); Serial.begin(9600); sendGSM("AT+SAPBR=3,1,\"APN\",\"zongwap\""); sendGSM("AT+SAPBR=1,1",300); sendGSM("AT+HTTPINIT"); sendGSM("AT+HTTPPARA=\"CID\",1"); sendGSM("AT+HTTPPARA=\"URL\",\"http://afridiofkh.000webhostapp.com? sensor1="" + sensor + ""\""""); sendGSM("AT+HTTPACTION=0"); } void loop() { while(GSM.available()) { parseATText(GSM.read()); } } void parseATText(byte b) { buffer[pos++] = b; if ( pos >= sizeof(buffer) ) resetBuffer(); // just to be safe /* // Detailed debugging Serial.println(); Serial.print("state = "); Serial.println(state); Serial.print("b = "); Serial.println(b); Serial.print("pos = "); Serial.println(pos); Serial.print("buffer = "); Serial.println(buffer);*/ switch (parseState) { case PS_DETECT_MSG_TYPE: { if ( b == '\n' ) resetBuffer(); else { if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) { parseState = PS_IGNORING_COMMAND_ECHO; } else if ( b == ':' ) { //Serial.print("Checking message type: "); //Serial.println(buffer); if ( strcmp(buffer, "+HTTPACTION:") == 0 ) { Serial.println("Received HTTPACTION"); parseState = PS_HTTPACTION_TYPE; } else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) { Serial.println("Received HTTPREAD"); parseState = PS_HTTPREAD_LENGTH; } resetBuffer(); } } } break; case PS_IGNORING_COMMAND_ECHO: { if ( b == '\n' ) { Serial.print("Ignoring echo: "); Serial.println(buffer); parseState = PS_DETECT_MSG_TYPE; resetBuffer(); } } break; case PS_HTTPACTION_TYPE: { if ( b == ',' ) { Serial.print("HTTPACTION type is "); Serial.println(buffer); parseState = PS_HTTPACTION_RESULT; resetBuffer(); } } break; case PS_HTTPACTION_RESULT: { if ( b == ',' ) { Serial.print("HTTPACTION result is "); Serial.println(buffer); parseState = PS_HTTPACTION_LENGTH; resetBuffer(); } } break; case PS_HTTPACTION_LENGTH: { if ( b == '\n' ) { Serial.print("HTTPACTION length is "); Serial.println(buffer); // now request content GSM.print("AT+HTTPREAD=0,"); GSM.println(buffer); parseState = PS_DETECT_MSG_TYPE; resetBuffer(); } } break; case PS_HTTPREAD_LENGTH: { if ( b == '\n' ) { contentLength = atoi(buffer); Serial.print("HTTPREAD length is "); Serial.println(contentLength); Serial.print("HTTPREAD content: "); parseState = PS_HTTPREAD_CONTENT; resetBuffer(); } } break; case PS_HTTPREAD_CONTENT: { // for this demo I'm just showing the content bytes in the serial monitor Serial.write(b); contentLength--; if ( contentLength <= 0 ) { // all content bytes have now been read parseState = PS_DETECT_MSG_TYPE; resetBuffer(); } } break; } } 添加了附加元素的方法。如果我想在特定索引处添加,该怎么做?

我尝试使用这种代码在第0个索引处添加元素。我正在寻找更好的东西,而无需实例化新的com.google.gson.JsonArray

JsonArray

2 个答案:

答案 0 :(得分:4)

由于JsonArray没有插入方法,这意味着你必须自己创建。它会在您选择的位置插入数组中的单个项目。

public static JsonArray insert(int index, JsonElement val, JsonArray currentArray) {
    JsonArray newArray = new JsonArray();
    for (int i = 0; i < index; i++) {
        newArray.add(currentArray.get(i));
    }
    newArray.add(val);

    for (int i = index; i < currentArray.size(); i++) {
        newArray.add(currentArray.get(i));
    }
    return newArray;
}

因此,使用此方法,将新项目0插入位置0的现有数组[1,2,3]:

insert(0, new JsonPrimitive(0), myArray);

在不改变原始数组的情况下,该方法将返回一个新数组[0,1,2,3]。希望有所帮助!

答案 1 :(得分:1)

我认为您正在寻找类似的东西,您可以使用下面提到的方法替换特定索引处的现有JsonElement。

JsonElement set(int index,JsonElement element) 用指定的元素替换此数组中指定位置的元素。

供参考:

https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonArray.html#set-int-com.google.gson.JsonElement-