Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
Deserialize JSON object array in C# using LitJson

I'm limited by Unity3d 3 new security measures to using LitJson for Web Player application, otherwise it won't compile.

Is there a simple way to serialize this output as an object (contents of userEcoGet.text):

{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}

This code only enters first object from JSON:

using UnityEngine;
using System.Collections;
using LitJson;
...
public EcoData localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData>(userEcoGet.text);
...
public class EcoData
{
    public string building_id;
    public string building_level;
    public string workers;
    public string building_health;
}

Turning localEcoData into array leads to missing constructor errors. And I just don't know how to nest constructor correctly in this situation, not even sure if it would help even.

At this point I'm resorting to using JsonReader and filling in object manually. Any help would be appreciated. Thank you.

Edit: That's just crazy, killed all day today just because of a crappy JSON formatting that I inflicted on myself...

This works perfectly now, localEcoData[1,2...32] is an object array with all elements accessible:

public EcoData[] localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData[]>(userEcoGet.text);

All I had to do is compose JSON correctly on PHP page which handles MySql and transfer back to Unity in orderly fashion. I was echoing directly without using an array for transfer. Now output looks like this and works great:

[{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}]

Thanks, man!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I suspect the problem is with your JSON itself. its not in correct format. To parse it as JSON Array you will need to have it in format:

{
    result:[
                {"building_id":"1","building_level":"0","workers":"0","building_health":"100"},
                {"building_id":"2","building_level":"0","workers":"0","building_health":"100"},...
                {"building_id":"32","building_level":"0","workers":"0","building_health":"100"}
           ]
}

If you dont have control on the JSON then you may need to split the string and treat each string as a single JSON object and Convert each element in the array to you C# object type.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda